bool Car::init(string fileName) { if (!Node::init()) return false; //------------- Khởi tạo sprite chính ------------- _sprite = Sprite::create(fileName); _sprite->setPosition(0, 0); _sprite->setAnchorPoint(Vec2(0.25, 0.25)); this->addChild(_sprite); //------------- Physic Body -------------- body = PhysicsBody::createBox(Size(_sprite->getContentSize().width / 2, _sprite->getContentSize().height / 2), PhysicsMaterial(100.0f, 0.0f, 100.0f), Vec2::ZERO); body->setDynamic(false); body->setTag(Tags::OBSTRUCTION); body->setCollisionBitmask(1); body->setContactTestBitmask(1); this->setAnchorPoint(Vec2::ANCHOR_MIDDLE); this->setPhysicsBody(body); //Mai xe auto nodeCar = Node::create(); nodeCar->setPosition(_sprite->getContentSize().width * 2.1 / 4, _sprite->getContentSize().height * 2.3 / 3); nodeCar->setAnchorPoint(Vec2::ANCHOR_MIDDLE); _sprite->addChild(nodeCar); PhysicsBody * bodyNodeCar = PhysicsBody::createBox(Size(_sprite->getContentSize().width * 2.5 / 5, _sprite->getContentSize().height / 3), PhysicsMaterial(100.0f, 0.0f, 100.0f), Vec2::ZERO); bodyNodeCar->setTag(Tags::MAIXE); bodyNodeCar->setDynamic(false); bodyNodeCar->setContactTestBitmask(1); bodyNodeCar->setCollisionBitmask(1); nodeCar->setPhysicsBody(bodyNodeCar); //Score auto nodeScore = Node::create(); nodeScore->setPosition(_sprite->getContentSize().width * 2.1 / 4, _sprite->getContentSize().height); nodeScore->setAnchorPoint(Vec2::ANCHOR_MIDDLE); _sprite->addChild(nodeScore); PhysicsBody * bodyNodeScore = PhysicsBody::createBox(Size(_sprite->getContentSize().width * 2.5 / 5, Config::screenSize.height), PhysicsMaterial(100.0f, 0.0f, 100.0f), Vec2::ZERO); bodyNodeScore->setTag(Tags::NODE_SCORE); bodyNodeScore->setDynamic(false); bodyNodeScore->setCategoryBitmask(0x01); bodyNodeScore->setContactTestBitmask(1); bodyNodeScore->setCollisionBitmask(0x02); nodeScore->setPhysicsBody(bodyNodeScore); return true; }
bool HeroSprite::init() { bool bRet = false; do { CC_BREAK_IF(!Sprite::initWithSpriteFrameName("heroStand_0001.png")); Size heroSize = this->getContentSize(); PhysicsBody *body = PhysicsBody::createBox(heroSize-Size(0,16), PhysicsMaterial(0, 0, 0)); body->setLinearDamping(0.0f); body->setDynamic(true); body->setGravityEnable(true); body->setTag(TAG_HERO_PHYS_BODY); body->setCategoryBitmask(1<<2); body->setContactTestBitmask(1<<0 | 1<<1); body->setMass(50); body->setRotationEnable(false); this->setPhysicsBody(body); mState = STATE_IDLE; mRunAnimate = NULL; mSmokeRunAnimate = NULL; bRet = true; } while(0); return bRet; }
bool guaiwu_js::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } auto root_layer = CSLoader::createNode("LayerUI/Other/guaiwu.csb"); addChild(root_layer); auto rooaby = root_layer->getChildByName<Node*>("FileNode_1")->getChildByName<Sprite*>("anim_guaiwu_zo0001_2"); PhysicsBody* phybody = PhysicsBody::createBox(Size(rooaby->getContentSize().width-8, rooaby->getContentSize().height-20)); phybody->setTag(400001); phybody->setMass(3000); phybody->setRotationEnable(false); phybody->setCategoryBitmask(0x01); phybody->setCollisionBitmask(0x02); phybody->setContactTestBitmask(0x01); phybody->setDynamic(true); rooaby->setPhysicsBody(phybody); guaiwu = CSLoader::createTimeline("Node/animation/guaiwujs.csb"); guaiwu->gotoFrameAndPlay(0,35, true); root_layer->runAction(guaiwu); scheduleUpdate(); //添加碰撞事件 auto collisionlistener = EventListenerPhysicsContact::create(); collisionlistener->onContactBegin =CC_CALLBACK_1(guaiwu_js::onCollisionBegin, this); collisionlistener->onContactSeparate = CC_CALLBACK_1(guaiwu_js::onContactSeparate,this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(collisionlistener, this); return true; }
bool Witch::init() { mWitch = CCSprite::createWithSpriteFrameName(Tex::Player_0); this->addChild(mWitch); CCLOG("Witch Width:%f", mWitch->getContentSize().width); mWitch->setPosition(0, 0); PhysicsBody *body = PhysicsBody::create(); body->setTag(1); this->setPhysicsBody(body); body->addShape(PhysicsShapeBox::create(Size(mWitch->getContentSize().width, mWitch->getContentSize().height))); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); return true; }
BoundWall::BoundWall(WallType type, Size screenSize) { PhysicsBody* body = nullptr; switch (type) { case UP: this->setAnchorPoint(Vec2(0.5f, 0)); this->setPosition(Vec2(screenSize.width / 2, screenSize.height)); body = PhysicsBody::createBox(Size(screenSize.width,5), PhysicsMaterial(1, 0, 0)); break; case DOWN: this->setAnchorPoint(Vec2(0.5f, 1)); this->setPosition(Vec2(screenSize.width / 2, 0)); body = PhysicsBody::createBox(Size(screenSize.width, 5), PhysicsMaterial(1, 0, 0)); break; case LEFT: this->setAnchorPoint(Vec2(1, 0.5f)); this->setPosition(Vec2(0, screenSize.height / 2)); body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0)); break; case RIGHT: this->setAnchorPoint(Vec2(0, 0.5f)); this->setPosition(Vec2(screenSize.width, screenSize.height / 2)); body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0)); break; default: break; } body->setGravityEnable(false); body->setDynamic(false); body->setTag(Tags::GROUND); body->setCollisionBitmask(true); body->setContactTestBitmask(true); this->setPhysicsBody(body); }
// 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; }
void FruitCutNinjaScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance) { PhysicsBody* body = shape->getBody(); int count = shape->getPointsCount(); int pointsCount = 0; Point* points = new Point[count + 1]; Vector2dVector vcPoints; vcPoints.clear(); Vector2d v2Point(0, 0); for (int i=0, j=count-1; i<count; j=i, ++i) { Point a = body->local2World(shape->getPoint(j)); float aDist = a.dot(normal) - distance; if (aDist < 0.0f) { points[pointsCount] = a; ++pointsCount; } Point b = body->local2World(shape->getPoint(i)); float bDist = b.dot(normal) - distance; if (aDist*bDist < 0.0f) { float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist)); Vec2 v2Tmp = a.lerp(b, t); points[pointsCount] = v2Tmp; ++pointsCount; } } Point center = PhysicsShape::getPolyonCenter(points, pointsCount); for (int i = 0; i < pointsCount; i++) { points[i] = body->world2Local(points[i]); vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount); PRFilledPolygon* pNode = (PRFilledPolygon*)(body->getNode()); std::string sName = pNode->getTextureName(); //auto texture = Director::getInstance()->getTextureCache()->addImage("pineapple.png"); PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, sName.c_str()); filledPolygon->setPhysicsBody(polyon); filledPolygon->setPosition(body->getPosition() + normal * -40); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); //polyon->applyImpulse(normal * -100); addChild(filledPolygon, 80); /* CPolygonSprite* pSprite = CPolygonSprite::create(); pSprite->initWithFile("pineapple.png", polyon, false); pSprite->setPosition(body->getPosition()); polyon->setTag(_sliceTag); addChild(pSprite); */ /* Node* node = Node::create(); PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount, PHYSICSBODY_MATERIAL_DEFAULT, -center); node->setPosition(center); node->setPhysicsBody(polyon); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); addChild(node); */ delete[] points; }
void CCutScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance) { PhysicsBody* body = shape->getBody(); int count = shape->getPointsCount(); int pointsCount = 0; Point* points = new Point[count + 1]; Vector2dVector vcPoints; vcPoints.clear(); Vector2d v2Point(0, 0); for (int i=0, j=count-1; i<count; j=i, ++i) { Point a = body->local2World(shape->getPoint(j)); float aDist = a.dot(normal) - distance; if (aDist < 0.0f) { points[pointsCount] = a; ++pointsCount; } Point b = body->local2World(shape->getPoint(i)); float bDist = b.dot(normal) - distance; if (aDist*bDist < 0.0f) { float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist)); Vec2 v2Tmp = a.lerp(b, t); points[pointsCount] = v2Tmp; ++pointsCount; } } Point center = PhysicsShape::getPolyonCenter(points, pointsCount); for (int i = 0; i < pointsCount; i++) { points[i] = body->world2Local(points[i]); vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount); CFoodCut* pNode = (CFoodCut*)(body->getNode()); std::vector<int> vMaterials; vMaterials.clear(); vMaterials = pNode->getMaterials(); MATERIAL_ID eId = MI_MAX; if (vMaterials.size() != 0) { eId = (MATERIAL_ID)vMaterials[0]; } CFoodCut *filledPolygon = CFoodCut::create(eId, vcPoints, pNode->getPanziIndex(), pNode->getTouchedIndex()); filledPolygon->setPhysicsBody(polyon); int nTmp = rand() % 50 + 50; int nTmpRotate = rand() % 30 - 60; filledPolygon->setPosition(body->getPosition()); //filledPolygon->setRotation(filledPolygon->getRotation() + nTmpRotate); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); //polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); float fMass = polyon->getMass(); float fV = 80; float fImpulse = fMass * fV; float fTmpX = (float)(Random() % 30) / 100.0f - 0.15f; float fTmpY = (float)(rand() % 30) / 100.0f - 0.15f; polyon->applyImpulse((normal + Vec2(fTmpX, fTmpY)) * -fImpulse); polyon->setLinearDamping(0.8f); addChild(filledPolygon, 80); filledPolygon->setBirthTime(getCurTime()); m_vCutFoods.push_back(filledPolygon); m_nSliceCount ++; delete[] points; }
// on "init" you need to initialize your instance bool Level_8::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 visiblePoint = Director::getInstance()->getVisibleOrigin(); root_level = CSLoader::createNode("Scene/LevelScene/Level_8.csb"); root_level->setTag(100001); this->addChild(root_level); //设置物理 空心世界 PhysicsBody* psworld =PhysicsBody::createEdgeBox(visibleSize*1.5,PHYSICSBODY_MATERIAL_DEFAULT,2); psworld->setCategoryBitmask(0x01); psworld->setCollisionBitmask(0x01); psworld->setContactTestBitmask(0x01); root_level->setPhysicsBody(psworld); //车 Sprite* maincar = root_level->getChildByName<Sprite*>("img_maincarbody_17"); //退出 Sprite* mainexit = root_level->getChildByName<Sprite*>("img_next_14"); //创建主要UI界面 mainui = MainUI::create(); auto maincar_r = MainCar_R::create(); mainui->setCar_R(maincar_r); maincar_r->setMainUI(mainui); maincar_r->setObj(maincar); addChild(maincar_r,10); addChild(mainui,3000); //退出 Exit* eit = Exit::create(); eit->setObj(mainexit); addChild(eit); //添加地面的刚体 Sprite* gound_1 = root_level->getChildByName<Sprite*>("l8_gound_1_1"); Sprite* gound_2 = root_level->getChildByName<Sprite*>("l8_gound_2_1"); Sprite* gound_3 = root_level->getChildByName<Sprite*>("l8_gound_3_1"); Sprite* gound_4 = root_level->getChildByName<Sprite*>("l8_gound_4_1"); Sprite* gound_5 = root_level->getChildByName<Sprite*>("l8_gound_5_1"); Sprite* gound_6 = root_level->getChildByName<Sprite*>("l8_gound_6_1"); Sprite* gound_7 = root_level->getChildByName<Sprite*>("l8_gound_7_1"); Sprite* gound_14 = root_level->getChildByName<Sprite*>("l8_gound_14_1"); ////////////////////////////////////////////////////////////////////// Sprite* gound_8 = root_level->getChildByName<Sprite*>("l8_gound_8_1"); Sprite* gound_9 = root_level->getChildByName<Sprite*>("l8_gound_9_1"); Sprite* gound_10 = root_level->getChildByName<Sprite*>("l8_gound_10_1"); Sprite* gound_11 = root_level->getChildByName<Sprite*>("l8_gound_11_1"); Sprite* gound_12 = root_level->getChildByName<Sprite*>("l8_gound_12_1"); Sprite* gound_13 = root_level->getChildByName<Sprite*>("l8_gound_13_1"); ToolsFunction::setPhyDynamicSpriteBox(gound_1); ToolsFunction::setPhyDynamicSpriteBox(gound_2); ToolsFunction::setPhyDynamicSpriteBox(gound_3); ToolsFunction::setPhyDynamicSpriteBox(gound_4); ToolsFunction::setPhyDynamicSpriteBox(gound_5); ToolsFunction::setPhyDynamicSpriteBox(gound_6); ToolsFunction::setPhyDynamicSpriteBox(gound_7); ToolsFunction::setPhyDynamicSpriteBox(gound_14); //四个二维数组点 组成多边形 Vec2 pa[3] = { Vec2(-gound_13->getContentSize().width/2,-gound_13->getContentSize().height/2), Vec2(-gound_13->getContentSize().width/2+2,gound_13->getContentSize().height/2-2), Vec2(-gound_13->getContentSize().width/3+120 ,-gound_13->getContentSize().height/2)}; auto aa = PhysicsBody::createPolygon(pa, 3); // aa->setCategoryBitmask(0x03); // aa->setCollisionBitmask(0x03); // aa->setContactTestBitmask(0x03); aa->setDynamic(false); //aa->setLinearDamping(5000.0f); gound_13->setPhysicsBody(aa); //按钮 1 Button* b1 = root_level->getChildByName<Button*>("Button_1"); Button* b2 = root_level->getChildByName<Button*>("Button_2"); Button* b3 = root_level->getChildByName<Button*>("Button_3"); Sprite* b4 = root_level->getChildByName<Sprite*>("Button_4"); Sprite* b5 = root_level->getChildByName<Sprite*>("Button_5"); //按钮问题 Sprite* q1 = root_level->getChildByName<Sprite*>("question_1"); Sprite* q2 = root_level->getChildByName<Sprite*>("question_2"); Sprite* q3 = root_level->getChildByName<Sprite*>("question_3"); Sprite* q4 = root_level->getChildByName<Sprite*>("question_4"); Sprite* q5 = root_level->getChildByName<Sprite*>("question_5"); //创建颜色按钮 4个按钮 QuestionButton* btn_1 = QuestionButton::create(); btn_1->setObj(b1); addChild(btn_1,5); QuestionButton* btn_2 = QuestionButton::create(); btn_2->setObj(b2); addChild(btn_2,5); QuestionButton* btn_3 = QuestionButton::create(); btn_3->setObj(b3); addChild(btn_3,5); Btn_Standard* btn_4 = Btn_Standard::create(); btn_4->setObj(b4); addChild(btn_4,5); Btn_Standard* btn_5 = Btn_Standard::create(); btn_5->setObj(b5); addChild(btn_5,5); //给按钮设置问题 4个问题 Btn_QuestionSelect* qbtn_1 = Btn_QuestionSelect::create(); qbtn_1->setObj(q1); qbtn_1->setIBtnMainUI(mainui, btn_1); addChild(qbtn_1,8); Btn_QuestionSelect* qbtn_2 = Btn_QuestionSelect::create(); qbtn_2->setObj(q2); qbtn_2->setIBtnMainUI(mainui, btn_2); addChild(qbtn_2,8); Btn_QuestionSelect* qbtn_3 = Btn_QuestionSelect::create(); qbtn_3->setObj(q3); qbtn_3->setIBtnMainUI(mainui, btn_3); addChild(qbtn_3,8); Btn_QuestionSelect* qbtn_4 = Btn_QuestionSelect::create(); qbtn_4->setObj(q4); qbtn_4->setIBtnMainUI(mainui, btn_4); addChild(qbtn_4,8); Btn_QuestionSelect* qbtn_5 = Btn_QuestionSelect::create(); qbtn_5->setObj(q5); qbtn_5->setIBtnMainUI(mainui, btn_5); addChild(qbtn_5,8); ParticleSystemQuad* qp5 = root_level->getChildByName<ParticleSystemQuad*>("Particle_2"); PhysicsBody* qpphy = PhysicsBody::createBox(Size(200.0f,10.0f)); qpphy->setPositionOffset(Vec2(-100.0f, 0.0f)); //火的碰撞掩码 qpphy->setTag(PHY_TAG_KILL_FIRE); //设置触发 掩码 qpphy->setContactTestBitmask(0x01); qpphy->setCollisionBitmask(0x02); qpphy->setDynamic(false); qp5->setPhysicsBody(qpphy); qp6 = root_level->getChildByName<ParticleSystemQuad*>("Particle_1"); PhysicsBody* qpphy6 = PhysicsBody::createBox(Size(60.0f,60.0f)); qpphy6->setPositionOffset(Vec2(0.0f, 0.0f)); //火的碰撞掩码 qpphy6->setTag(PHY_TAG_KILL_FIRE); //设置触发 掩码 qpphy6->setContactTestBitmask(0x01); qpphy6->setCollisionBitmask(0x02); qpphy6->setDynamic(true); qpphy6->setRotationEnable(false); qp6->setPhysicsBody(qpphy6); //车变换位置 mainui->setMovePosition(gound_8, gound_9,true); //添加怪物 Scorpion_gw* scor = Scorpion_gw::create(); scor->setObj(gound_11); scor->setMovePosition(gound_10,gound_12, 26.2f); addChild(scor); //弹簧 Sprite* th = root_level->getChildByName<Sprite*>("tur0001_91"); objSpring* ths = objSpring::create(); ths->setPhysicsTag(600002); ths->setObj(th); addChild(ths); //弹簧 Sprite* th1 = root_level->getChildByName<Sprite*>("tur0001_91_1"); objSpring* ths1 = objSpring::create(); ths1->setPhysicsTag(600003); ths1->setObj(th1); addChild(ths1); Layer* ly = Layer::create(); ly->addChild(gound_7); addChild(ly); //冰块 Node* iceoo = root_level->getChildByName<Node*>("FileNode_2"); iceoo->setContentSize(Size(80.0f,200.0f)); PhysicsBody* phyice = PhysicsBody::createBox(Size(80.0f,200.0f)); phyice->setTag(PHY_TAG_ICE); phyice->setPositionOffset(Vec2(-30.0f, -80.0f)); phyice->setDynamic(false); phyice->setCategoryBitmask(0x03); phyice->setCollisionBitmask(0x03); phyice->setContactTestBitmask(0x03); iceoo->setPhysicsBody(phyice); Layer* iceot = Layer::create(); iceot->addChild(iceoo); addChild(iceot,12); auto coillsEvent = EventListenerPhysicsContact::create(); coillsEvent->onContactBegin = CC_CALLBACK_1(Level_8::onCollisionBegin, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(coillsEvent, this); //按钮添加事件 btn_1->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { btn_1->getBtn()->loadTextureNormal("DesertUI/Level/level_7/animation/btnopen0002.png"); auto tato = MoveTo::create(1.0f, Vec2(469.0f, gound_2->getPosition().y)); gound_2->runAction(tato); } }); btn_2->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (gound_13->getRotation() >= 0.0f && gound_13->getRotation() <= 80.0f) { btn_2->getBtn()->loadTextureNormal("DesertUI/Level/level_8/btn_fire0001.png"); auto sx = RotateTo::create(1.0f,90.0f); gound_13->runAction(sx); }else{ btn_2->getBtn()->loadTextureNormal("DesertUI/Level/level_8/btn_fire0002.png"); auto sx = RotateTo::create(1.0f,0.0f); gound_13->runAction(sx); } } }); btn_3->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { btn_3->getBtn()->loadTextureNormal("DesertUI/Level/level_7/animation/btnopen0002.png"); auto rotato = RotateTo::create(3.0f, 28.0f); gound_6->runAction(rotato); } }); btn_4->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { ths1->addForce(Vec2(-260.0f, 300.0f)); } }); btn_5->getBtn()->addTouchEventListener([=](Ref* reft, Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { ths->addForce(Vec2(-260.0f, 300.0f)); } }); Sprite* carmask = root_level->getChildByName<Sprite*>("img_maincarbody_17_0"); carmask->setVisible(false); masklayer = Layer::create(); masklayer->setContentSize(carmask->getContentSize()); masklayer->addChild(carmask); addChild(masklayer,10); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [](Touch* touch, Event* event){ /* get the target bind by the touch event listener */ auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point pos = Director::getInstance()->convertToGL(touch->getLocationInView()); /* judge if the touch position inside the bounding box of sprite */ if (target->getBoundingBox().containsPoint(pos)) { /* set the opacity of the sprite */ //target->setOpacity(100); return true; } return false; }; Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, carmask); 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; }