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; }
void SpeedUpProtect::initBody() { auto s = getContentSize(); Vec2 vec[7] = { Vec2(0,0), Vec2(s.width * 0.1, s.height * 0.4), Vec2(s.width * 0.26, s.height * 0.72), Vec2(s.width * 0.5, s.height), Vec2(s.width * 0.74, s.height * 0.72), Vec2(s.width * 0.9, s.height * 0.4), Vec2(s.width,0), }; PhysicsBody* body = PhysicsBody::createPolygon(vec, 7, PHYSICSBODY_MATERIAL_DEFAULT, Vec2(-s.width*0.5,-s.height*0.5)); body->setGravityEnable(false); body->setRotationEnable(false); body->setDynamic(false); body->setMass(10000000); body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK); body->setCollisionBitmask(SELF_SCOPE_COLLISIONBITMASK); body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK); setPhysicsBody(body); setInvincible(true); m_ContactPro.hurts = -Ene_Plane_Hp_max - 10; }
bool Monster::init() { mutex = PTHREAD_MUTEX_INITIALIZER; status = DONE; auto snakeTexture = TextureCache::sharedTextureCache()->addImage( basic.getCString()); float dWidth = snakeTexture->getContentSize().width / cntWidth; float dHeight = snakeTexture->getContentSize().height / cntHeight; this->setAnchorPoint(Vec2(0.5,32/dHeight)); PhysicsBody *body = PhysicsBody::createBox(Size(60,60)); body->setPositionOffset(Vec2(0,64/4-dHeight/4)); body->setRotationEnable(false); body->setGravityEnable(false); body->setContactTestBitmask(0xffffffff); body->setCollisionBitmask(0xffffffff); this->setPhysicsBody(body); this->setTag(MONSTER); auto listener = EventListenerCustom::create("main_action", CC_CALLBACK_1(Monster::receive,this)); _eventDispatcher->addEventListenerWithFixedPriority(listener, 1); return true; return false; }
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; }
void Person::addPhysics() { auto size = (this->getBoundingBox()).size; log("%lf %lf", size.width, size.height); auto material = PhysicsMaterial(100.0f, 0.01f, 1.0f); if(_type == TYPE::HERO) size.width /= 2; PhysicsBody *body = PhysicsBody::createBox(Size(size.width,size.height),material); // body->addShape(PhysicsShapeBox::create(Size(size.width,size.height),material)); body->setCategoryBitmask(_type); if(_type == TYPE::MONSTER) { body->setCollisionBitmask(TYPE::MONSTER | TYPE::BRICK | TYPE::GROUND | TYPE::TANGH | TYPE::BULLET | TYPE::BOSS | TYPE::PLANK); body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::BRICK | TYPE::TANGH | TYPE::BULLET | TYPE::BOSS); } else if(_type == TYPE::HERO) { body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::PLANK); body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::BULLET | TYPE::TRAP | TYPE::BOSS | TYPE::BULLETENEMY | TYPE::PLANK | TYPE::BUFF); } else if(_type == TYPE::BOSS) { body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS | PLANK); body->setContactTestBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS); } body->setDynamic(true); body->setLinearDamping(0.0f); body->setRotationEnable(false); body->setGravityEnable(true); this->setPhysicsBody(body); }
void BoySprite::setPhysical() { // add the physical body PhysicsBody *body = PhysicsBody::create(); body->addShape(NORMAL_SHAPE); body->setDynamic(true); body->setLinearDamping(0.0f); body->setRotationEnable(false); this->setPhysicsBody(body); }
void Hero::initPhysics() { PhysicsBody* pbody = nullptr; pbody = PhysicsBody::createBox(getSize()); pbody->setDynamic(true); pbody->setRotationEnable(false); pbody->setMoment(0); pbody->setMass(0.8); pbody->setContactTestBitmask(1); pbody->setGravityEnable(false); getSprite()->setPhysicsBody(pbody); mEnableGravity = true; mEnableForceField = true; }
bool Buff::init(int type) { char txt[100]; _type = type; sprintf(txt, "buff%d.png", type); if (!Sprite::initWithFile(txt)) { return false; } this->setTag(TYPE::BUFF); auto size = (this->getBoundingBox()).size; auto material = PhysicsMaterial(100.0f, 0.01f, 1.0f); PhysicsBody *body = PhysicsBody::createBox(Size(size.width,size.height),material); // body->addShape(PhysicsShapeBox::create(Size(size.width,size.height),material)); body->setCategoryBitmask(TYPE::BUFF); body->setCollisionBitmask(0); body->setContactTestBitmask(TYPE::BUFF | TYPE::HERO); body->setDynamic(false); body->setRotationEnable(false); this->setPhysicsBody(body); 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; }
bool HelloWorld::init(PhysicsWorld* world,int _level) { if ( !Layer::init() ) { return false; } level = _level; //NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(HelloWorld::setLevel), "selectedLevel", NULL); dispatcher = Director::getInstance()->getEventDispatcher(); preLoadMusic(); playBgMusic(); m_world = world; //m_world->setAutoStep(false); m_world->setGravity(Vect(0,-grivity)); this->setAnchorPoint(Point(0,0)); winSize = Director::getInstance()->getWinSize(); visibleSize = Director::getInstance()->getVisibleSize(); origin = Director::getInstance()->getVisibleOrigin(); auto map1path = String::createWithFormat("%s%d%s","images/level",level,"/bg.png"); map1 = Sprite::create(map1path->getCString()); map1->setPosition(map1->getContentSize().width/2+origin.x, map1->getContentSize().height/2+origin.y); this->addChild(map1,1,MAP1_TAG); auto map2path = String::createWithFormat("%s%d%s", "images/level", level, "/bg.png"); map2 = Sprite::create(map2path->getCString()); map2->setPosition(map1->getContentSize().width+map2->getContentSize().width/2+origin.x, map2->getContentSize().height/2+origin.y); this->addChild(map2,1,MAP2_TAG); Sprite* edge1 = Sprite::create(); edge1->setContentSize(visibleSize); PhysicsBody* edgeBody1 = PhysicsBody::createEdgeBox(visibleSize); edgeBody1->setDynamic(false); edgeBody1->setGravityEnable(false); edgeBody1->setMass(100000.0f); edgeBody1->getShape(0)->setRestitution(0.0f); edgeBody1->setContactTestBitmask(0xFF); edgeBody1->setCategoryBitmask(0x04); edgeBody1->setCollisionBitmask(0xFF); edgeBody1->getShape(0)->setTag(EDGE_TAG); edge1->setPhysicsBody(edgeBody1); edge1->setPosition(visibleSize.width/2,visibleSize.height/2); addChild(edge1,1,EDGEONE_TAG); Sprite* edge = Sprite::create(); edge->setContentSize(visibleSize); PhysicsBody* edgeBody = PhysicsBody::createEdgeBox(Size(visibleSize.width,visibleSize.height/6)); edgeBody->setDynamic(false); edgeBody->setGravityEnable(false); edgeBody->setMass(100000.0f); edgeBody->getShape(0)->setRestitution(0.0f); edgeBody->setContactTestBitmask(0xFF); edgeBody->setCategoryBitmask(0x04); edgeBody->setCollisionBitmask(0xFF); edgeBody->getShape(0)->setTag(EDGE_TAG); edge->setPhysicsBody(edgeBody); edge->setPosition(visibleSize.width / 2, visibleSize.height / 12); addChild(edge, 1, EDGE_TAG); Sprite* floor = Sprite::create(); PhysicsBody* floorBody = PhysicsBody::createEdgeSegment(Point(0, visibleSize.height / 10), Point(visibleSize.width, visibleSize.height / 10)); floorBody->setDynamic(false); floorBody->setGravityEnable(false); floorBody->getShape(0)->setRestitution(0.0f); floorBody->setMass(100000.0f); floorBody->setCategoryBitmask(0x0003); floorBody->setContactTestBitmask(0x0003); floorBody->setCollisionBitmask(0x0001); floor->setPosition(0, visibleSize.height / 10); floor->setPhysicsBody(floorBody); //this->addChild(floor,2,FLOOR_TAG); setEnemyFrameNumber(); setPlayerFrameNumber(); initDamage(); player1 = new player(); auto playerPath = String::createWithFormat("%s%d%s", "images/level", level, "/player.png"); player1->sprite = Sprite::create(playerPath->getCString()); PhysicsBody* playerBody = PhysicsBody::createBox(player1->sprite->getContentSize()); playerBody->setRotationEnable(false); playerBody->getShape(0)->setRestitution(0.0f); playerBody->getShape(0)->setFriction(0.8f); playerBody->setMass(player1->mass); playerBody->setCollisionBitmask(0x04); playerBody->setCategoryBitmask(0x01); playerBody->setContactTestBitmask(0xFF); playerBody->getShape(0)->setTag(PLAYER_TAG); player1->sprite->setPhysicsBody(playerBody); player1->sprite->setPosition(player1->sprite->getContentSize().width/2+5, floor->getPositionY()+player1->sprite->getContentSize().height*3); addChild(player1->sprite,2,PLAYER_TAG); preLoadAnimation(); auto backItem = MenuItemImage::create("images/CloseNormal.png", "images/CloseSelected.png", CC_CALLBACK_0(HelloWorld::backToChoseLevel, this)); //auto pauseItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_0(HelloWorld::pauseScene, this)); auto popupItem = MenuItemImage::create("images/CloseNormal.png", "images/CloseSelected.png", CC_CALLBACK_0(HelloWorld::toPopup, this)); //pauseItem->setPosition(-backItem->getContentSize().width,0); popupItem->setPosition(0,0); backItem->setPosition(backItem->getContentSize().width,0); auto backmenu = Menu::create(popupItem,backItem,NULL); backmenu->setPosition(visibleSize.width - backItem->getContentSize().width*2, visibleSize.height - backItem->getContentSize().height / 2); addChild(backmenu, 10); hpLayer = HPLayer::create(); hpLayer->setPosition(0, visibleSize.height / 15 * 14); addChild(hpLayer, 2, HPLAYER_TAG); Vector<MenuItem*> attackLogoItems; MenuItemSprite* attackLogoItem; for (int i = 0; i < ATTACKNUMBER; i++){ auto logoPath = String::createWithFormat("%s%d%s%d%s", "images/level", level, "/attack", i, "/logo.png"); auto normalSprite = Sprite::create(logoPath->getCString()); normalSprite->setOpacity(200); auto selectedSprite = Sprite::create(logoPath->getCString()); attackLogoItem = MenuItemSprite::create(normalSprite, selectedSprite, CC_CALLBACK_0(player::setAttack, player1, i)); attackLogoItem->setPosition(Vec2(0, -i*attackLogoItem->getContentSize().height)); attackLogoItems.pushBack(attackLogoItem); } auto attackLogoMenu = Menu::createWithArray(attackLogoItems); attackLogoMenu->setPosition(visibleSize.width - attackLogoItem->getContentSize().width, visibleSize.height / 2 + 2 * attackLogoItem->getContentSize().height); addChild(attackLogoMenu, 10); onTouchEvent(); onKeyBoardEvent(); physicsEvent(); EventCustom e("custom"); dispatcher->dispatchEvent(&e); enemyComing(1.0f); this->schedule(schedule_selector(HelloWorld::enemyComing), 5.0f); this->schedule(schedule_selector(HelloWorld::enemyAttack), 2.0f); this->scheduleUpdate(); log("init"); return true; }
void SinglePlayerScene::setupPhysics() { Size visibleSize = Director::getInstance()->getVisibleSize(); float visibleHeight = visibleSize.height; PhysicsWorld* world = this->getScene()->getPhysicsWorld(); world->setGravity(Vec2(0, (visibleHeight / 320.0f) * -98.0f)); world->setSpeed(GAME_SPEED); world->setDebugDrawMask(DEBUGDRAW_OPTIONS); Vec2 takyanPos = _takyan->getPosition(); PhysicsBody *takyanBody = PhysicsBody::createCircle(_takyan->getContentSize().width * BALL_RADIUS, PhysicsMaterial(0.01f, 1.0f, 1.0f)); //_takyan->getPhysicsBody(); float takyanMass = takyanBody->getMass(); float velocityLimit = takyanMass * 150.0f; takyanBody->setMass(takyanMass * 100.0f); takyanBody->setRotationEnable(false); takyanBody->setVelocityLimit(velocityLimit); _takyan->setPhysicsBody(takyanBody); PHYSICS_MASK(takyanBody, MASK_TAKYAN, MASK_BOUNDS | MASK_KICKER, MASK_BOUNDS | MASK_KICKER); Size takyanSize = _takyan->getContentSize(); takyanSize.height *= 0.8f; PhysicsMaterial material(0.0025f, 0.0f, 1.0f); PhysicsBody *takyanTailBody = PhysicsBody::createCircle(takyanSize.width, material); takyanTailBody->setMass(takyanMass * 0.01); takyanTailBody->setRotationEnable(false); takyanTailBody->setLinearDamping(1.0f); takyanTailBody->setVelocityLimit(velocityLimit * 0.5f); _takyanTail->setPhysicsBody(takyanTailBody); PHYSICS_MASK(takyanTailBody, MASK_TAKYAN_TAIL, 0, MASK_FLOOR); Vec2 diff = _takyanTail->getPosition() - _takyan->getPosition(); Vec2 anch1 = Vec2::ANCHOR_MIDDLE; Vec2 anch2 = Vec2::ANCHOR_MIDDLE; auto jointS = PhysicsJointLimit::construct(takyanBody, takyanTailBody, anch1, anch2, 0, diff.y); jointS->setCollisionEnable(false); world->addJoint(jointS); PhysicsBody *kickerBody = PhysicsBody::createCircle(_kicker->getContentSize().width * KICKER_RADIUS, PhysicsMaterial(1.0f, 1.0f, 1.0f)); //_kicker->getPhysicsBody(); kickerBody->setDynamic(false); kickerBody->setEnable(false); _kicker->setPhysicsBody(kickerBody); PHYSICS_MASK(kickerBody, MASK_KICKER, MASK_TAKYAN, MASK_TAKYAN); int takyans = MASK_TAKYAN | MASK_TAKYAN_TAIL; auto floorBody = PhysicsBody::createBox(_floorBounds->getContentSize(), PhysicsMaterial(1.0f, 0.3f, 0.7f)); //_floorBounds->getPhysicsBody(); floorBody->setDynamic(false); _floorBounds->setPhysicsBody(floorBody); PHYSICS_MASK(floorBody, MASK_BOUNDS, takyans, takyans); auto ceilingBody = PhysicsBody::createBox(_ceilingBounds->getContentSize(), PhysicsMaterial(1.0f, 0.3f, 0.7f)); ceilingBody->setDynamic(false); _ceilingBounds->setPhysicsBody(ceilingBody); PHYSICS_MASK(ceilingBody, MASK_BOUNDS, MASK_TAKYAN, MASK_TAKYAN); Size wallSize = _leftWallBounds->getContentSize(); wallSize.height *= 5.0f; auto leftWallBody = PhysicsBody::createBox(wallSize); //_leftWallBounds->getPhysicsBody(); leftWallBody->setDynamic(false); _leftWallBounds->setPhysicsBody(leftWallBody); PHYSICS_MASK(leftWallBody, MASK_WALLS, takyans, MASK_TAKYAN); auto rightWallBody = PhysicsBody::createBox(wallSize); //_rightWallBounds->getPhysicsBody(); rightWallBody->setDynamic(false); _rightWallBounds->setPhysicsBody(rightWallBody); PHYSICS_MASK(rightWallBody, MASK_WALLS, takyans, MASK_TAKYAN); }