/** 碰撞开始事件 * @2015/12/16 11:28 */ bool Level_1::onCollisionBegin(const cocos2d::PhysicsContact& contact) { PhysicsBody* bodyA = contact.getShapeA()->getBody(); PhysicsBody* bodyB = contact.getShapeB()->getBody(); PhysicsBody* temp =nullptr; //log("A:%d B:%d",bodyA->getTag(),bodyB->getTag()); if(bodyA->getTag() == 555555) { temp = bodyB; bodyB = bodyA; bodyA = temp; } if(bodyB->getTag() == 555555) { //log("A:%d",bodyA->getTag()); // 100001 物理世界 // 555555 主要车的 // 300001 悬浮的物体 // 400001 第一关的怪物 // 600001 弹簧的物体 if (bodyA->getTag()== 400001) { MainCar_R* car =(MainCar_R*)bodyB->getNode()->getParent()->getParent(); //car->removeFromParent(); car->setBodyEnable(); } switch (bodyA->getTag()) { case 400001: log("第一关的怪物"); bodyB->setEnabled(false); break; case 300001: log("悬浮的物体"); break; case 600001: log("弹簧的物体"); break; default: break; } } return true; }
// on "init" you need to initialize your instance bool Level_5::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ){ return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 visiblePoint = Director::getInstance()->getVisibleOrigin(); auto root_level_5 = CSLoader::createNode("Scene/LevelScene/Level_5.csb"); root_level_5->setTag(100001); this->addChild(root_level_5); //设置物理 空心世界 PhysicsBody* psworld =PhysicsBody::createEdgeBox(Size(visibleSize.width,visibleSize.height*1.2),PHYSICSBODY_MATERIAL_DEFAULT,2); psworld->setCategoryBitmask(0x01); psworld->setCollisionBitmask(0x01); psworld->setContactTestBitmask(0x01); root_level_5->setPhysicsBody(psworld); //火把 Node* hb1 = root_level_5->getChildByName<Node*>("FileNode_1"); //火把 Node* hb2 = root_level_5->getChildByName<Node*>("FileNode_1_0"); fire_x* fhb1 = fire_x::create(); fhb1->setObject(hb1); addChild(fhb1); fire_x* fhb2 = fire_x::create(); fhb2->setObject(hb2); addChild(fhb2); //完成退出 Sprite* exitl5 = root_level_5->getChildByName<Sprite*>("img_next_27"); //主要车 Sprite* carl5 = root_level_5->getChildByName<Sprite*>("img_maincarbody_1"); //按钮一 Sprite* btnl5_1 = root_level_5->getChildByName<Sprite*>("btn_010001_50"); //按钮二 anim_l5_2 = root_level_5->getChildByName<Node*>("FileNode_2_kill"); //按钮三 Sprite* btnl5_3 = root_level_5->getChildByName<Sprite*>("btn_010001_50_0"); //问题1 Sprite* questionl4_1 = root_level_5->getChildByName<Sprite*>("anim_question0001_51_0"); //问题2 Sprite* questionl4_2 = root_level_5->getChildByName<Sprite*>("anim_question0001_51"); //问题3 Sprite* questionl4_3 = root_level_5->getChildByName<Sprite*>("anim_question0001_25"); //怪物a Sprite* monsterl5_0 = root_level_5->getChildByName<Sprite*>("monster_18"); //怪物 Sprite* monsterl5_1 = root_level_5->getChildByName<Sprite*>("anim_guaiwu_1"); //按钮二 Sprite* monsterl5_2 = root_level_5->getChildByName<Sprite*>("btn_010001_22_0"); //按钮二 Sprite* monsterl5_3 = root_level_5->getChildByName<Sprite*>("btn_010001_22_0_0"); //地面按钮 Sprite* btngroundl5_2 = root_level_5->getChildByName<Sprite*>("btn_dicar0001_9"); //地面右上 Sprite* groundl5_1 = root_level_5->getChildByName<Sprite*>("td4_47_2"); //地面下 Sprite* groundl5_2 = root_level_5->getChildByName<Sprite*>("d1_3"); //地面左上 Sprite* groundl5_5 = root_level_5->getChildByName<Sprite*>("td4_47_1"); //地面右上 Sprite* groundl5_6 = root_level_5->getChildByName<Sprite*>("td4_47_3"); //弹簧 Sprite* sprs = root_level_5->getChildByName<Sprite*>("tur0001_47"); //怪物门 Sprite* gate = root_level_5->getChildByName<Sprite*>("d3_5"); //yidong Sprite* move_17 = root_level_5->getChildByName<Sprite*>("move_17"); //电网 ParticleSystemQuad* particle_1 = root_level_5->getChildByName<ParticleSystemQuad*>("Particle_1"); particle_1->setContentSize(Size(26.0f,200.0f)); ToolsFunction::setPhyDynamicSpriteBox(move_17); ToolsFunction::setPhyDynamicSpriteBox(groundl5_1); ToolsFunction::setPhyDynamicSpriteBox(groundl5_2); ToolsFunction::setPhyDynamicSpriteBox(groundl5_5); ToolsFunction::setPhyDynamicSpriteBox(groundl5_6); //设置刚体 PhysicsBody* psworld2 =PhysicsBody::createBox(Size(particle_1->getContentSize().width,particle_1->getContentSize().height)); psworld2->setPositionOffset(Vec2(-13.0f, -110.0f)); //火的碰撞掩码 psworld2->setTag(PHY_TAG_KILL_FIRE); //设置触发 掩码 psworld2->setContactTestBitmask(0x01); psworld2->setCollisionBitmask(0x02); psworld2->setDynamic(false); particle_1->setPhysicsBody(psworld2); //添加地面按钮 Btn_Ground_1* btngr = Btn_Ground_1::create(); btngr->setObj(btngroundl5_2); addChild(btngr); //创建主要UI界面 mainui = MainUI::create(); auto maincar_r = MainCar_R::create(); mainui->setCar_R(maincar_r); maincar_r->setMainUI(mainui); maincar_r->setObj(carl5); addChild(maincar_r,10); addChild(mainui,3000); //退出 Exit* eit = Exit::create(); eit->setObj(exitl5); addChild(eit); Btn_QuestionSelect* qbs2 = Btn_QuestionSelect::create(); qbs2->setObj(questionl4_1); qbs2->setIBtnMainUI(mainui, btngr); addChild(qbs2,30); //弹簧按按钮 Btn_Standard* bs2 = Btn_Standard::create(); bs2->setObj(btnl5_1); addChild(bs2,30); Btn_QuestionSelect* qbs3 = Btn_QuestionSelect::create(); qbs3->setObj(questionl4_2); qbs3->setIBtnMainUI(mainui, bs2); addChild(qbs3,30); //怪物,门 Btn_Standard* bs3 = Btn_Standard::create(); bs3->setObj(btnl5_3); addChild(bs3,30); Btn_QuestionSelect* qbs4 = Btn_QuestionSelect::create(); qbs4->setObj(questionl4_3); qbs4->setIBtnMainUI(mainui, bs3); addChild(qbs4,30); Layer* dlyer = Layer::create(); dlyer->addChild(groundl5_5); addChild(dlyer); //怪物移动 guaiwu_js* gu = guaiwu_js::create(); gu->setGuanwu(monsterl5_1); gu->setMovePosition(monsterl5_2, monsterl5_3, 20.0f); addChild(gu); PhysicsBody* gua = PhysicsBody::createBox(monsterl5_0->getContentSize()); gua->setTag(400030); gua->setRotationEnable(false); gua->setCategoryBitmask(0x01); gua->setCollisionBitmask(0x02); gua->setContactTestBitmask(0x01); gua->setDynamic(true); monsterl5_0->setPhysicsBody(gua); //添加力度 bs2->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type==Widget::TouchEventType::ENDED) { //bs2->PlayEffect(); //spr->addForce(Vec2(1000.0f, 1500.0f)); if (ismove) { auto moveto = MoveTo::create(0.8f, Vec2(384.2f, move_17->getPosition().y)); move_17->runAction(moveto); ismove = false; }else{ auto moveto = MoveTo::create(0.8f, Vec2(159.2f, move_17->getPosition().y)); move_17->runAction(moveto); ismove = true; } } }); //关门 bs3->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type==Widget::TouchEventType::ENDED) { bs2->PlayEffect(); auto scal = ScaleTo::create(1.0f, -1, 1); gate->runAction(scal); gua->setEnabled(false); } }); //地面按钮 btngr->onCallbackTest_2([=]() { if (particle_1) { particle_1->setVisible(false); particle_1->getPhysicsBody()->setEnabled(false); } }); //怪物动画刚体 Sprite* sprsphys = root_level_5->getChildByName<Sprite*>("btn_010001_22_1"); sprsphys->setVisible(false); PhysicsBody* guaup = PhysicsBody::createBox(Size(sprsphys->getContentSize().width*1.5f,sprsphys->getContentSize().height)); guaup->setTag(PHY_TAG_KILL_UP); guaup->setRotationEnable(false); guaup->setCategoryBitmask(0x01); guaup->setCollisionBitmask(0x02); guaup->setContactTestBitmask(0x01); guaup->setDynamic(true); sprsphys->setPhysicsBody(guaup); //播放怪物动画 anim = CSLoader::createTimeline("Node/level_animation/l5_updown.csb"); anim->gotoFrameAndPlay(0, 320, true); root_level_5->runAction(anim); //怪物下来 anim->addFrameEndCallFunc(29, "a", [=]() { guaup->setEnabled(true); }); //怪物下来 anim->addFrameEndCallFunc(30, "b", [=]() { guaup->setEnabled(true); }); //怪物下来 anim->addFrameEndCallFunc(74, "c", [=]() { guaup->setEnabled(false); }); //怪物下来 anim->addFrameEndCallFunc(75, "d", [=]() { guaup->setEnabled(false); }); scheduleUpdate(); return true; }
// on "init" you need to initialize your instance bool Level_2::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 visiblePoint = Director::getInstance()->getVisibleOrigin(); auto root_level_2 = CSLoader::createNode("Scene/LevelScene/Level_2.csb"); root_level_2->setTag(100001); this->addChild(root_level_2); //设置物理 空心世界 PhysicsBody* psworld =PhysicsBody::createEdgeBox(visibleSize*1.5,PHYSICSBODY_MATERIAL_DEFAULT,2); psworld->setCategoryBitmask(0x01); psworld->setCollisionBitmask(0x01); psworld->setContactTestBitmask(0x01); root_level_2->setPhysicsBody(psworld); //素材火把添加; auto hb1 = root_level_2->getChildByName<Sprite*>("FileNode_1_0"); auto hb2 = root_level_2->getChildByName<Sprite*>("FileNode_1"); //添加一个火把 auto ohb1 = fire_x::create(); ohb1->setObject(hb1); root_level_2->addChild(ohb1); //添加二个火把 auto ohb2 = fire_x::create(); ohb2->setObject(hb2); root_level_2->addChild(ohb2); //设置 物理世界里面的 静态 刚体 Sprite* phys1 = root_level_2->getChildByName<Sprite*>("img_ground_04"); Sprite* phys2 = root_level_2->getChildByName<Sprite*>("img_ground_05_25"); Sprite* phys3 = root_level_2->getChildByName<Sprite*>("img_ground_06"); ToolsFunction::setPhyDynamicSpriteBox(phys1); ToolsFunction::setPhyDynamicSpriteBox(phys2); ToolsFunction::setPhyDynamicSpriteBox(phys3); //设置多边形的 物理刚体 Sprite* phys4 = root_level_2->getChildByName<Sprite*>("img_ground_03"); //四个二维数组点 组成多边形 Vec2 pa[4] = { Vec2(-phys4->getContentSize().width/2,-phys4->getContentSize().height/2), Vec2(-phys4->getContentSize().width/2,phys4->getContentSize().height/2-2), Vec2(-phys4->getContentSize().width/3+150 ,phys4->getContentSize().height/2-2), Vec2(phys4->getContentSize().width/2,-phys4->getContentSize().height/2)}; auto aa = PhysicsBody::createPolygon(pa, 4); aa->setDynamic(false); aa->setLinearDamping(5000.0f); phys4->setPhysicsBody(aa); //退出的 Sprite* ni1 = root_level_2->getChildByName<Sprite*>("img_next_27"); auto nexit = Exit::create(); nexit->setObj(ni1); addChild(nexit); //火的精灵 Sprite* fire1 = root_level_2->getChildByName<Node*>("FileNode_6")->getChildByName<Sprite*>("anim_huo0001_1"); //设置刚体 PhysicsBody* psworld2 =PhysicsBody::createBox(Size(fire1->getContentSize().width-40,fire1->getContentSize().height-20)); //火的碰撞掩码 psworld2->setTag(PHY_TAG_KILL_FIRE); //设置触发 掩码 psworld2->setContactTestBitmask(0x01); psworld2->setCollisionBitmask(0x02); psworld2->setDynamic(false); fire1->setPhysicsBody(psworld2); //帧动画 不能使用精灵运行帧动画 auto fireh1 = CSLoader::createTimeline("Node/animation/L2_fire.csb"); fireh1->gotoFrameAndPlay(0, 33, true); root_level_2->runAction(fireh1); //获取车的位置 Sprite* mycar = root_level_2->getChildByName<Sprite*>("img_maincarbody_9"); //创建主要UI界面 mainui = MainUI::create(); auto maincar = MainCar_R::create(); mainui->setCar_R(maincar); maincar->setMainUI(mainui); maincar->setObj(mycar); addChild(maincar); addChild(mainui,3000); //添加第一个怪物 //FileNode_5 Node* jsa1 = root_level_2->getChildByName<Node*>("FileNode_5"); Sprite* psa1 = root_level_2->getChildByName<Sprite*>("btn_010001_59_0_0"); Sprite* psa2 = root_level_2->getChildByName<Sprite*>("btn_010001_59_0_0_0"); Sprite* psa3 = root_level_2->getChildByName<Sprite*>("btn_010001_59_2"); psa3->setVisible(false); //第一个怪物 guaiwu_js* gw = guaiwu_js::create(); gw->setGuanwu(jsa1); gw->setMovePosition(psa2, psa1, 18.0f); addChild(gw); //获取肉的精灵 Sprite* rm = root_level_2->getChildByName<Sprite*>("s22_43"); Sprite* r = root_level_2->getChildByName<Sprite*>("s23_50"); r->setVisible(false); //设置肉的碰撞刚体 PhysicsBody* phyr = PhysicsBody::createBox(Size(r->getContentSize().width/3,r->getContentSize().height-3)); phyr->setEnabled(false); //设置触发 掩码 phyr->setContactTestBitmask(0x01); phyr->setCollisionBitmask(0x02); phyr->setTag(400006); phyr->setRotationEnable(true); r->setPhysicsBody(phyr); //phyr->setCategoryBitmask(0x01); //火的按钮 Sprite* h1 = root_level_2->getChildByName<Sprite*>("btn_010001_59_1"); //实物的按钮 Sprite* h2 = root_level_2->getChildByName<Sprite*>("btn_010001_59"); //火的 问题 按钮 Sprite* qh1 = root_level_2->getChildByName<Sprite*>("anim_question0001_60_0"); //实物 问题 的按钮 Sprite* qh2 = root_level_2->getChildByName<Sprite*>("anim_question0001_60"); //按钮1 Btn_Standard* hba1 =Btn_Standard::create(); hba1->setObj(h1); addChild(hba1); hba1->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type ==Widget::TouchEventType::ENDED) { hba1->PlayEffect(); //控制火的按钮 fireh1->gotoFrameAndPause(36); fire1->getPhysicsBody()->setEnabled(false); } }); //按钮2 Btn_Standard* hba2 =Btn_Standard::create(); hba2->setObj(h2); addChild(hba2); hba2->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type ==Widget::TouchEventType::ENDED) { hba1->PlayEffect(); //控制肉的按钮 //gw->setEat(); hba2->getBtn()->setEnabled(false); rm->setVisible(false); r->setVisible(true); r->getPhysicsBody()->setEnabled(true); gw->Reverse(r); //更换怪物的移动位置 gw->setMovePosition(psa3, psa1, 68.0f); } }); //第一个 按钮的 问题 Btn_QuestionSelect* qhba1 = Btn_QuestionSelect::create(); qhba1->setObj(qh1); qhba1->setIBtnMainUI(mainui, hba1); addChild(qhba1); //第二个 按钮的 问题 Btn_QuestionSelect* qhba2 = Btn_QuestionSelect::create(); qhba2->setObj(qh2); qhba2->setIBtnMainUI(mainui, hba2); addChild(qhba2); //添加地雷 Node* nh2 = root_level_2->getChildByName<Node*>("FileNode_8"); Bombed* bm = Bombed::create(); bm->setObj(nh2); bm->setBombbri(phys3); addChild(bm); Sprite* qbh2 = root_level_2->getChildByName<Sprite*>("anim_question0001_61"); //第二个 按钮的 问题 Btn_QuestionSelect* qhbba2 = Btn_QuestionSelect::create(); qhbba2->setObj(qbh2); qhbba2->setIBtnMainUI(mainui, bm); addChild(qhbba2); return true; }