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 Tree::addToParent(Node* parent) { parent -> addChild( mLeavesSprite); mLeavesSprite->setTag(1); PhysicsBody* mLeavesBody = PhysicsBody::createCircle(mLeavesSize.width / 4, PHYSICSBODY_MATERIAL_DEFAULT); mLeavesBody->setContactTestBitmask(1); mLeavesBody->setCollisionBitmask(0); mLeavesBody->setCategoryBitmask(1); mLeavesNode -> setPhysicsBody( mLeavesBody); parent->addChild(mLeavesNode); mLeavesNode->setPosition(mLeavesPosition); parent -> addChild( mBodySprite); mBodySprite->setTag(1); PhysicsBody* mBodyBody = PhysicsBody::createBox(mBodySize, PHYSICSBODY_MATERIAL_DEFAULT); mBodyBody->setContactTestBitmask(1); mBodyBody->setCollisionBitmask(0); mBodyBody->setCategoryBitmask(1); mBodyNode -> setPhysicsBody( mBodyBody); parent->addChild(mBodyNode); mBodyNode->setPosition(mPosition); parent -> addChild( mRootSprite); mLeavesSprite->setAnchorPoint(Vec2(0.5, -0.8)); mBodySprite -> setAnchorPoint(Vec2(0.5, 0)); mBodySprite -> setPosition(Vec2(mBodyPosition.x, mBodyPosition.y - mBodySprite->getContentSize().height / 2)); mLeavesSprite -> setPosition(mBodySprite->getPosition()); Rotation(); }
void PipeSprite::configPipeRandom() { Size visiableSize = Director::getInstance()->getVisibleSize(); Vec2 visiableOrigin = Director::getInstance()->getVisibleOrigin(); /**********************config upper pipe********************************/ mUpperPipe->setPosition(Vec2(0, mPipeAccessHeight + 60)); upperPipeExpand->setPosition(mUpperPipe->getPosition().x, mUpperPipe->getContentSize().height); upperPipeExpand->setScaleY((visiableSize.height - mUpperPipe->getPosition().y - mUpperPipe->getContentSize().height) / upperPipeExpand->getContentSize().height); /**********************config upper pipe********************************/ /**********************config bottom pipe********************************/ mBottomPipe->setPosition(Vec2(0, mPipeAccessHeight - 60)); bottomPipeExpand->setPosition(0, -mBottomPipe->getPosition().y + mLandHeight); bottomPipeExpand->setScaleY((mBottomPipe->getPosition().y - mLandHeight) / bottomPipeExpand->getContentSize().height); /**********************config bottom pipe********************************/ /****************add upper physics body*******************/ if (mUpperPipe->getPhysicsBody() != nullptr) { mUpperPipe->removeComponent(mUpperPipe->getPhysicsBody()); } PhysicsBody * newUpperPipeBody = PhysicsBody::createBox(Size(mUpperPipe->getContentSize().width, visiableSize.height - mUpperPipe->getPosition().y), PhysicsMaterial(1.0, 1.0, 0)); newUpperPipeBody->setPositionOffset(Vec2(0, (visiableSize.height - mUpperPipe->getPosition().y - mUpperPipe->getContentSize().height) / 2)); newUpperPipeBody->setGravityEnable(false); newUpperPipeBody->setDynamic(false); newUpperPipeBody->setContactTestBitmask(1); newUpperPipeBody->setCollisionBitmask(2); mUpperPipe->setPhysicsBody(newUpperPipeBody); /****************add upper physics body*******************/ /****************add bottom physics body*******************/ if (mBottomPipe->getPhysicsBody() != nullptr) { mBottomPipe->removeComponent(mBottomPipe->getPhysicsBody()); } PhysicsBody * newBottomPipeBody = PhysicsBody::createBox(Size(mBottomPipe->getContentSize().width, mBottomPipe->getPosition().y - mLandHeight + mBottomPipe->getContentSize().height), PhysicsMaterial(1.0, 1.0, 0)); newBottomPipeBody->setPositionOffset(Vec2(0, -(mBottomPipe->getPosition().y - mLandHeight) / 2)); newBottomPipeBody->setGravityEnable(false); newBottomPipeBody->setDynamic(false); newBottomPipeBody->setContactTestBitmask(1); newBottomPipeBody->setCollisionBitmask(2); mBottomPipe->setPhysicsBody(newBottomPipeBody); /****************add bottom physics body*******************/ }
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 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 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; }
void ShowLayer::initPhyscsObject(Rect* m) { for (int i = 0; i < 26; i++) { Sprite* sp = Sprite::create(); auto maskLayer = LayerColor::create(Color4B(0, 0, 255, 200)); maskLayer->setContentSize(m[i].size); maskLayer->setAnchorPoint(Vec2(0, 0)); sp->addChild(maskLayer, 15); PhysicsBody* playerBody = PhysicsBody::createBox(m[i].size, PHYSICSBODY_MATERIAL_DEFAULT); playerBody->setDynamic(false); //设置质量 playerBody->getShape(0)->setMass(100); //设置物体是否受重力系数影响 playerBody->setGravityEnable(false); playerBody->setCategoryBitmask(3); playerBody->setContactTestBitmask(3); playerBody->setCollisionBitmask(3); sp->setContentSize(m[i].size); sp->setPosition(Vec2(m[i].getMidX(), m[i].getMidY())); sp->setAnchorPoint(Vec2(0.0, 0.0)); sp->setPhysicsBody(playerBody); this->addChild(sp); } }
void PFMPlayer::reload() { if(_preset == NULL) return; for(int i=0;i<_preset->components.size();i++) { PFMComponent* component = _preset->components[i]; if(component->componentClass == "Sprite") { PFMSpriteComponent* spriteComponent = dynamic_cast<PFMSpriteComponent*>(component); PFMSprite* sprite = PFMSprite::createWithComponent(spriteComponent); Size size = sprite->getBoundingBox().size; PhysicsBody* physicsBody = PhysicsBody::createBox(sprite->getBoundingBox().size); physicsBody->setCollisionBitmask(0); physicsBody->setCategoryBitmask(PFMCollideMaskBitsPlayer); physicsBody->setContactTestBitmask(PFMCollideMaskBitsEnemyBullet | PFMCollideMaskBitsEnemy); setPhysicsBody(physicsBody); addChild(sprite); } else if(component->componentClass == "BulletGun") { PFMBulletGunComponent* bulletGunComponent = dynamic_cast<PFMBulletGunComponent*>(component); PFMBulletGun* bulletGun = PFMBulletGun::createWithComponent(bulletGunComponent); bulletGun->isHostByPlayer = true; bulletGun->isAutoShoot = true; addChild(bulletGun); } // else if([component isMemberOfClass:[AstMissleLauncherComponent class]]) // { // AstMissleLauncher* missleLauncher = [[AstMissleLauncher alloc]initWithSession:self.session]; // [self addNode:missleLauncher.rootNode]; // [missleLauncher setComponent:(AstMissleLauncherComponent*)component]; // } } }
void Monster::joinToWorld(Node* parent) { Sprite* sp = Sprite::createWithSpriteFrameName(m_sModelPath.asString().c_str()); /* 创建刚体 */ PhysicsBody* body = PhysicsBody::createBox(sp->getContentSize()); body->setCategoryBitmask(1); // 0001 body->setCollisionBitmask(1); // 0001 body->setContactTestBitmask(1); // 0001 /* 精灵居中 */ sp->setPosition(Point(sp->getContentSize().width * 0.5f, sp->getContentSize().height * 0.5f)); /* 精灵作为Monster的表现,添加到Monster身上 */ this->addChild(sp); /* 设置怪物Tag类型 */ this->setTag(ObjectTag_Monster); /* 精灵作为Monster的表现,Monster本身没有大小,所以要设置一下大小 */ this->setContentSize(sp->getContentSize()); /* 刚体添加到Monster本身,而不是精灵身上 */ this->setPhysicsBody(body); /* 设置坐标 */ this->setPosition(Point(getiPosX(), getiPosY())); /* Monster加入到物理世界 */ parent->addChild(this); }
bool LandSprite::init() { if (!Sprite::init()) { return false; } Size visiableSize = Director::getInstance()->getVisibleSize(); Vec2 visiableOrigin = Director::getInstance()->getVisibleOrigin(); Sprite * landSprite1 = Sprite::create("assets/land.png"); landSprite1->setAnchorPoint(Vec2(0, 0)); landSprite1->setPosition(Vec2(visiableOrigin.x, visiableOrigin.y)); landSprite1->setScaleX(visiableSize.width / landSprite1->getContentSize().width / 2); Sprite * landSprite2 = Sprite::create("assets/land.png"); landSprite2->setAnchorPoint(Vec2(0, 0)); landSprite2->setPosition(Vec2(visiableOrigin.x + visiableSize.width / 2, visiableOrigin.y)); landSprite2->setScaleX(visiableSize.width / landSprite2->getContentSize().width / 2); Sprite * landSprite3 = Sprite::create("assets/land.png"); landSprite3->setAnchorPoint(Vec2(0, 0)); landSprite3->setPosition(Vec2(visiableOrigin.x + visiableSize.width, visiableOrigin.y)); landSprite3->setScaleX(visiableSize.width / landSprite3->getContentSize().width / 2); Sprite * landSprite4 = Sprite::create("assets/land.png"); landSprite4->setAnchorPoint(Vec2(0, 0)); landSprite4->setPosition(Vec2(visiableOrigin.x + visiableSize.width * 1.5, visiableOrigin.y)); landSprite4->setScaleX(visiableSize.width / landSprite4->getContentSize().width / 2); Node * landAnimNode = Node::create(); // node hold the land animation landAnimNode->addChild(landSprite1); landAnimNode->addChild(landSprite2); landAnimNode->addChild(landSprite3); landAnimNode->addChild(landSprite4); MoveBy * landForward = MoveBy::create(5, Vec2(-visiableSize.width, 0)); MoveBy * landBackward = MoveBy::create(0.001, Vec2(visiableSize.width, 0)); Sequence * moveSeq = Sequence::create(landForward, 0.001, landBackward,NULL); landSprite1->runAction(RepeatForever::create(moveSeq)); landSprite2->runAction(RepeatForever::create(moveSeq->clone())); landSprite3->runAction(RepeatForever::create(moveSeq->clone())); landSprite4->runAction(RepeatForever::create(moveSeq->clone())); this->addChild(landAnimNode); this->setAnchorPoint(Vec2(0, 0)); this->setContentSize(Size(visiableSize.width, landSprite1->getContentSize().height)); PhysicsBody * landPhysicsBody = PhysicsBody::createBox(Size(visiableSize.width, landSprite1->getContentSize().height), PhysicsMaterial(1, 1, 0)); landPhysicsBody->setGravityEnable(false); landPhysicsBody->setDynamic(false); landPhysicsBody->setContactTestBitmask(1); landPhysicsBody->setCollisionBitmask(2); this->setPhysicsBody(landPhysicsBody); return true; }
bool Rock::init() { if (!Sprite::initWithFile("rock.png")) { return false; } PhysicsBody* rockBody = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(DEFAULT_ROCK_MATERIAL)); rockBody->setDynamic(false); rockBody->setContactTestBitmask(ROCK_CONTACT_MASK); rockBody->setCategoryBitmask(ROCK_CATEGORY); rockBody->setCollisionBitmask(ROCK_COLLISION_MASK); rockBody->setGravityEnable(false); this->setPhysicsBody(rockBody); return true; }
/* auto ball = Sprite::create("Ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBird); ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip); ballbody->setContactTestBitmask(1); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(true); ball->setPhysicsBody(ballbody); auto visibleSize = Director::getInstance()->getVisibleSize(); ball->setPosition(bird->getPositionX()+2*20,visibleSize.height); addChild(ball); */ void GameLayer::addBallObstacle(float dt) { auto ball = Sprite::create("Ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBall); ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBird | ColliderTypeBall); ballbody->setContactTestBitmask(ColliderTypeBird); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(true); ball->setPhysicsBody(ballbody); auto visibleSize = Director::getInstance()->getVisibleSize(); ball->setPosition(bird->getPositionX()+2*20,visibleSize.height); addChild(ball); }
CSpikes :: CSpikes(cocos2d::Layer* layer,const float posx,const float posy) { x = posx; y = posy; m_Sprite = cocos2d::Sprite::create("CloseNormal.png"); //m_Sprite->Sprite::setTexture("CloseNormal.png"); m_Sprite->setScale(10.0f); m_Sprite->Sprite::setPosition(Point(x,y)); const Size size = Size(m_Sprite->getContentSize().width, m_Sprite->getContentSize().width); PhysicsBody* body = PhysicsBody::createBox(size); body->setCollisionBitmask(2); body->setContactTestBitmask(true); body->setDynamic(false); m_Sprite->setPhysicsBody(body); Render(x,y); layer->addChild(m_Sprite,0); thelayer = layer; }
void MonsterPart::init(Monster * _parent, cocos2d::Layer * layer, cocos2d::Vec2 _pos , std::string spriteName, float _sInterval , int zLayer , bool ifPhysics , BulletType _bulletType) { // init parameter if (_parent != NULL) { setPartent(_parent); }else parent = NULL; shootInterval = _sInterval; this->bulletType = _bulletType; // set up the sprite pos = _pos; sprite = Sprite::create(spriteName); sprite->setScale(0.5); if (parent != NULL) { sprite->setPosition(parent->getRelatedPos(pos)); sprite->setColor(parent->color); } layer->addChild(sprite, zLayer); // if (ifPhysics) { // 1 auto monsterSize = sprite->getContentSize(); PhysicsBody* body = PhysicsBody::createBox(Size(monsterSize.width , monsterSize.height),PhysicsMaterial(0.1f, 1.0f, 0.0f)); // 2 body->setDynamic(true); // 3 body->setCategoryBitmask(PhysicsCategory::Part); body->setCollisionBitmask((int)PhysicsCategory::None); body->setContactTestBitmask((int)PhysicsCategory::MonsterA); UserData* data = new UserData(); data->part = this; sprite->setUserData((void*)data); sprite->setPhysicsBody(body); } }
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; }
CLaser :: CLaser(cocos2d::Layer* layer,const float posx,const float posy) { index = 0;//default index x = posx; y = posy; m_Sprite = cocos2d::Sprite::create("CloseNormal.png"); empty = CCTextureCache::sharedTextureCache()->addImage("empty.png"); //m_Sprite->Sprite::setTexture("CloseNormal.png"); m_Sprite->setScale(10.0f); m_Sprite->Sprite::setPosition(Point(x,y)); const Size size = Size(m_Sprite->getContentSize().width, m_Sprite->getContentSize().width); PhysicsBody* body = PhysicsBody::createBox(size); body->setCollisionBitmask(3); body->setContactTestBitmask(true); body->setDynamic(false); m_Sprite->setPhysicsBody(body); Render(x,y); layer->addChild(m_Sprite,0); }
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_6::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 visiblePoint = Director::getInstance()->getVisibleOrigin(); auto root_level = CSLoader::createNode("Scene/LevelScene/Level_6.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_27"); //旋转物体 Sprite* rota = root_level->getChildByName<Sprite*>("gound_6_6"); //创建主要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); addChild(mainui,3000); //退出 Exit* eit = Exit::create(); eit->setObj(mainexit); addChild(eit); //添加地面的刚体 Sprite* gound_1 = root_level->getChildByName<Sprite*>("gound_5_5"); Sprite* gound_2 = root_level->getChildByName<Sprite*>("gound_3_4"); Sprite* gound_3 = root_level->getChildByName<Sprite*>("gound_3_3"); Sprite* gound_4 = root_level->getChildByName<Sprite*>("gound_6_6"); Sprite* gound_5 = root_level->getChildByName<Sprite*>("gound_1_1"); ToolsFunction::setPhyDynamicSpriteBox(gound_2); ToolsFunction::setPhyDynamicSpriteBox(gound_3); ToolsFunction::setPhyDynamicSpriteBox(gound_4); ToolsFunction::setPhyDynamicSpriteBox(gound_5); //四个二维数组点 组成多边形 Vec2 pa[6] = { Vec2(-gound_1->getContentSize().width/2,gound_1->getContentSize().height/2-72), Vec2(-gound_1->getContentSize().width/2,gound_1->getContentSize().height/2-2), Vec2(-gound_1->getContentSize().width/3+198 ,gound_1->getContentSize().height/2-2), Vec2(gound_1->getContentSize().width/2-2,-gound_1->getContentSize().height/2+81), Vec2(gound_1->getContentSize().width/2-2,-gound_1->getContentSize().height/2), }; PhysicsBody* aa = PhysicsBody::createPolygon(pa, 5); aa->setDynamic(false); aa->setLinearDamping(5000.0f); gound_1->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"); Button* b4 = root_level->getChildByName<Button*>("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"); //Sprite* q6 = root_level->getChildByName<Sprite*>("ar_50"); //创建颜色按钮 4个按钮 QuestionButton* btn_1 = QuestionButton::create(); btn_1->setObj(b1); addChild(btn_1); QuestionButton* btn_2 = QuestionButton::create(); btn_2->setObj(b2); addChild(btn_2); QuestionButton* btn_3 = QuestionButton::create(); btn_3->setObj(b3); addChild(btn_3); QuestionButton* btn_4 = QuestionButton::create(); btn_4->setObj(b4); addChild(btn_4); Btn_Standard* btn_5 = Btn_Standard::create(); btn_5->setObj(b5); addChild(btn_5); //给按钮设置问题 4个问题 Btn_QuestionSelect* qbtn_1 = Btn_QuestionSelect::create(); qbtn_1->setObj(q1); qbtn_1->setIBtnMainUI(mainui, btn_1); addChild(qbtn_1); Btn_QuestionSelect* qbtn_2 = Btn_QuestionSelect::create(); qbtn_2->setObj(q2); qbtn_2->setIBtnMainUI(mainui, btn_2); addChild(qbtn_2); Btn_QuestionSelect* qbtn_3 = Btn_QuestionSelect::create(); qbtn_3->setObj(q3); qbtn_3->setIBtnMainUI(mainui, btn_3); addChild(qbtn_3); Btn_QuestionSelect* qbtn_4 = Btn_QuestionSelect::create(); qbtn_4->setObj(q4); qbtn_4->setIBtnMainUI(mainui, btn_4); addChild(qbtn_4); Btn_QuestionSelect* qbtn_5 = Btn_QuestionSelect::create(); qbtn_5->setObj(q5); qbtn_5->setIBtnMainUI(mainui, btn_5); addChild(qbtn_5); //4个按钮添加点击事件 btn_1->getBtn()->addTouchEventListener([=](Ref* reft , Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = -47.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_2->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = -10.0f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_3->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = 27.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_4->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = 65.0f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_5->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { auto mov = RotateTo::create(0.8f, 29.22f); rota->runAction(mov); } }); //射箭动画 Node* animanode = root_level->getChildByName<Node*>("FileNode_1"); auto archery = CSLoader::createTimeline("Node/animation/anim_Archery.csb"); archery->gotoFrameAndPlay(0, 155, true); root_level->runAction(archery); archery->addFrameEndCallFunc(95, "a", [=]() { Archery* a = Archery::create(); a->setPosition(Vec2(animanode->getPositionX(), animanode->getPositionY())); addChild(a); }); return true; }
bool GameLayer::init() { if (!Layer::init()) { return false; } this->score = 0; this->gameStatus = GameStatus::GAME_RUNNING; this->bestScore = UserDefault::getInstance()->getIntegerForKey(KEY); visibleSize = Director::getInstance()->getVisibleSize(); //创建小鸟 this->bird = BirdSprite::getInstance(); this->bird->createBird(); //创建物理属性 PhysicsBody *body = PhysicsBody::createCircle(15); //设置为受到重力影响的动态刚体 body->setDynamic(true); //设置线性阻尼 body->setLinearDamping(0.0f); //设置刚体是否受物理世界重力的影响 body->setGravityEnable(false); //设置形状的恢复系数 //body->getShape(0)->setRestitution(0.0f); body->getShape(0)->setDensity(1.0f); //设置所属种类的掩码值 body->setCategoryBitmask(BIRD_MASK); body->setCollisionBitmask(BIRD_MASK | OBST_MASK); body->setContactTestBitmask(BIRD_MASK | OBST_MASK); this->bird->setPhysicsBody(body); this->bird->setPosition(visibleSize.width / 3, visibleSize.height / 2); this->bird->idle(); this->bird->fly(); this->addChild(this->bird, 2); //添加陆地物理属性 auto land = Node::create(); landHeight = BackgroundLayer::getLandHeight(); auto landBody = PhysicsBody::createBox(Size(visibleSize.width, landHeight)); landBody->getShape(0)->setRestitution(0.0f); landBody->setDynamic(false); landBody->setGravityEnable(false); landBody->setCategoryBitmask(OBST_MASK); landBody->setCollisionBitmask(BIRD_MASK | OBST_MASK); landBody->setContactTestBitmask(BIRD_MASK | OBST_MASK); land->setPhysicsBody(landBody); land->setPosition(visibleSize.width / 2, landHeight / 2); this->addChild(land, 10); //添加碰撞监听 auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); this->createPips(); this->scheduleUpdate(); this->schedule(schedule_selector(GameLayer::scrollPipe), 0.01f); return true; }
// 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; }
void LevelFifteen::callbackC(Node* sender) { auto visibleSize = Director::getInstance()->getVisibleSize(); auto pictureName = 1 + rand() % 27; auto str = String::createWithFormat("%d.png", pictureName)->getCString(); auto ballTwo2 = Sprite::createWithSpriteFrameName(str); ballTwo2->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 90)); PhysicsBody* ballBodyTwo = nullptr; if (pictureName <= 9) { ballBodyTwo = PhysicsBody::createBox(ballTwo2->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); } else if (pictureName > 9 && pictureName <= 18) { ballBodyTwo = PhysicsBody::createCircle(ballTwo2->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); } else if (pictureName > 18 && pictureName <= 21) { Vec2 vec[] = { Vec2(-22.50000, 22.00000), Vec2(24.00000, 10.00000), Vec2(-10.00000, -24.00000) }; ballBodyTwo = PhysicsBody::createPolygon(vec, 3); } else if (pictureName > 21 && pictureName <= 24) { Vec2 vec[] = { Vec2(-15.50000, 15.00000), Vec2(15.50000, 6.00000), Vec2(-7.00000, -15.50000) }; ballBodyTwo = PhysicsBody::createPolygon(vec, 3); } else if (pictureName > 24 && pictureName <= 27) { Vec2 vec[] = { Vec2(-8.00000, 8.00000), Vec2(7.50000, 3.00000), Vec2(-4.00000, -7.50000) }; ballBodyTwo = PhysicsBody::createPolygon(vec, 3); } //是否设置物体为静态 //ballBodyTwo->setDynamic(false); ballBodyTwo->getShape(0)->setRestitution(1.0f); ballBodyTwo->getShape(0)->setFriction(0.0f); ballBodyTwo->getShape(0)->setDensity(1.0f); ballBodyTwo->setGravityEnable(false); ballBodyTwo->setCategoryBitmask(2);// 分类掩码 ballBodyTwo->setCollisionBitmask(1 | 2 | 4 | 8);// 碰撞掩码 ballBodyTwo->setContactTestBitmask(1 | 8);// 接触测试掩码 ballBodyTwo->addMass(1000.0f); int flag = CCRANDOM_0_1() * 2; auto num = 0; switch (flag) { case 0: num = -1; break; case 1: num = 1; break; default: break; } auto force1 = Vec2(CCRANDOM_0_1()*10000.0f*num, -300000.0f); ballBodyTwo->applyImpulse(force1); ballTwo2->setPhysicsBody(ballBodyTwo); this->addChild(ballTwo2); }
// 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; }
// on "init" you need to initialize your instance bool Level_1::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } auto root_level_1 = CSLoader::createNode("Scene/LevelScene/Level_1.csb"); root_level_1->setTag(100001); this->addChild(root_level_1); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 visiblePoint = Director::getInstance()->getVisibleOrigin(); //获取车的位置 Sprite* mycar = root_level_1->getChildByName<Sprite*>("btn_010001_64_1_1"); //创建车的实例 auto maincar = MainCar_R::create(); maincar->setObj(mycar); //创建主要UI界面 mainui = MainUI::create(); //添加车到UI里 mainui->setCar_R(maincar); mainui->setTag(PHY_TAG_MANUI); maincar->setMainUI(mainui); this->addChild(maincar); addChild(mainui,3000); //素材火把添加; auto hb1 = root_level_1->getChildByName<Sprite*>("FileNode_2_0"); auto hb2 = root_level_1->getChildByName<Sprite*>("FileNode_2"); //添加一个火把 auto ohb1 = fire_x::create(); ohb1->setObject(hb1); root_level_1->addChild(ohb1); //添加二个火把 auto ohb2 = fire_x::create(); ohb2->setObject(hb2); root_level_1->addChild(ohb2); //添加第一个怪物 auto gw1 = root_level_1->getChildByName<Sprite*>("FileNode_5"); auto gw2 = root_level_1->getChildByName<Sprite*>("btn_010001_64"); auto gw3 = root_level_1->getChildByName<Sprite*>("btn_010001_64_0"); auto gw = guaiwu_js::create(); gw->setGuanwu(gw1); gw->setDelete_js(gw); gw->setMovePosition(gw2, gw3, 28.0f); addChild(gw); //添加第二个怪物 auto gw21 = root_level_1->getChildByName<Sprite*>("FileNode_5_0"); auto gw22 = root_level_1->getChildByName<Sprite*>("btn_010001_64_1"); auto gw23 = root_level_1->getChildByName<Sprite*>("btn_010001_64_1_0"); auto gwgw = guaiwu_js::create(); gwgw->setGuanwu(gw21); gwgw->setDelete_js(gwgw); gwgw->setMovePosition(gw22, gw23, 20.0f); addChild(gwgw); auto sprite1 = root_level_1->getChildByName<Sprite*>("img_ground_01"); auto sprite2 = root_level_1->getChildByName<Sprite*>("img_ground_02"); ToolsFunction::setPhySpriteBox(sprite1); ToolsFunction::setPhySpriteBox(sprite2); auto sprite4 = root_level_1->getChildByName<Sprite*>("img_next_6"); ToolsFunction::setPhyContactSpriteBox(sprite4); sprite4->getPhysicsBody()->setTag(PHY_TAG_FINISHLEVEL); //设置世界世界空心刚体 PhysicsBody* psworld =PhysicsBody::createEdgeBox(visibleSize*2,PHYSICSBODY_MATERIAL_DEFAULT,2); psworld->setCategoryBitmask(0x01); psworld->setCollisionBitmask(0x01); psworld->setContactTestBitmask(0x01); root_level_1->setPhysicsBody(psworld); //添加问题按钮 auto a1 = root_level_1->getChildByName<Sprite*>("btn_010001_1"); auto a2 = root_level_1->getChildByName<Sprite*>("btn_010001_2"); auto a3 = root_level_1->getChildByName<Sprite*>("btn_010001_3"); auto qq1 = root_level_1->getChildByName<Sprite*>("anim_question0001_12"); auto qq2 = root_level_1->getChildByName<Sprite*>("anim_question0001_13"); auto qq3 = root_level_1->getChildByName<Sprite*>("anim_question0001_14"); //控制物体按钮 1; Btn_Standard* btn1 = Btn_Standard::create(); btn1->setName("btn_1"); btn1->setObj(a1); btn1->getBtn()->addTouchEventListener(CC_CALLBACK_2(Level_1::onBtnClick, this)); addChild(btn1,29); //控制物体按钮 2; Btn_Standard* btn2 = Btn_Standard::create(); btn2->setName("btn_2"); btn2->setObj(a2); btn2->getBtn()->addTouchEventListener(CC_CALLBACK_2(Level_1::onBtnClick, this)); addChild(btn2,29); //控制物体按钮 3; Btn_Standard* btn3 = Btn_Standard::create(); btn3->setName("btn_3"); btn3->setObj(a3); btn3->getBtn()->addTouchEventListener(CC_CALLBACK_2(Level_1::onBtnClick, this)); addChild(btn3,29); //出问题按钮 1; Btn_QuestionSelect* ques1 = Btn_QuestionSelect::create(); ques1->setObj(qq1); ques1->setIBtnMainUI(mainui, btn1); addChild(ques1,30); //出问题按钮 2; Btn_QuestionSelect* ques2 = Btn_QuestionSelect::create(); ques2->setObj(qq2); ques2->setIBtnMainUI(mainui, btn2); addChild(ques2,30); //出问题按钮 3; Btn_QuestionSelect* ques3 = Btn_QuestionSelect::create(); ques3->setObj(qq3); ques3->setIBtnMainUI(mainui, btn3); addChild(ques3,30); //创建悬浮器 auto x = root_level_1->getChildByName<Sprite*>("FileNode_4"); auto x1 = root_level_1->getChildByName<Sprite*>("FileNode_4_0"); log("初始位置 %f %f",x1->getPosition().x,x1->getPosition().y); xd1 = other_level_1::create(); xd1->setObj(x); this->addChild(xd1); xd2 = other_level_1::create(); xd2->setObj(x1); this->addChild(xd2); //设置两个悬浮器 一个可以用。另一个不可以用 xd1->getNo()->getPhysicsBody()->setEnabled(true); xd2->getNo()->getPhysicsBody()->setEnabled(false); xd1->setVisible(true); xd2->setVisible(false); //设置弹跳 auto tan = root_level_1->getChildByName<Sprite*>("tur0001_91"); os = objSpring::create(); os->setObj(tan); addChild(os); Layer* grlayer = Layer::create(); auto ground = root_level_1->getChildByName<Sprite*>("img_ground_02"); grlayer->addChild(ground); addChild(grlayer); this->scheduleUpdate(); return true; }
bool GameLayer::init(){ if(Layer::init()) { //get the origin point of the X-Y axis, and the visiable size of the screen Size visiableSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); this->gameStatus = GAME_STATUS_READY; this->score = 0; // Add the bird this->bird = BirdSprite::getInstance(); this->bird->createBird(); PhysicsBody *body = PhysicsBody::create(); body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); //ÅöײÉèÖà body->setCategoryBitmask(ColliderTypeBird); body->setCollisionBitmask(ColliderTypeLand | ColliderTypePip); body->setContactTestBitmask(ColliderTypeLand | ColliderTypePip); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); this->bird->setPhysicsBody(body); this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5); this->bird->idle(); this->addChild(this->bird); // Add the ground this->groundNode = Node::create(); float landHeight = BackgroundLayer::getLandHeight(); auto groundBody = PhysicsBody::create(); groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); //ÅöײÉèÖà groundBody->setCategoryBitmask(ColliderTypeLand); groundBody->setCollisionBitmask(ColliderTypeBird); groundBody->setContactTestBitmask(ColliderTypeBird); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(144, landHeight/2); this->addChild(this->groundNode); // init land this->landSpite1 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite1->setAnchorPoint(Point::ZERO); this->landSpite1->setPosition(Point::ZERO); this->addChild(this->landSpite1, 30); this->landSpite2 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite2->setAnchorPoint(Point::ZERO); this->landSpite2->setPosition(this->landSpite1->getContentSize().width-2.0f,0); this->addChild(this->landSpite2, 30); shiftLand = schedule_selector(GameLayer::scrollLand); this->schedule(shiftLand, 0.01f); this->scheduleUpdate(); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); this->dialogLayer = nullptr; return true; }else { return false; } }
bool GameLayer::init(){ if(Layer::init()) { //get the origin point of the X-Y axis, and the visiable size of the screen Size visiableSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); this->gameStatus = GAME_STATUS_READY; this->score = 0; // Add the bird this->bird = BirdSprite::getInstance(); this->bird->createBird(); PhysicsBody *body = PhysicsBody::create(); body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); body->setCategoryBitmask(ColliderTypeBird); body->setCollisionBitmask(ColliderTypeLand & ColliderTypePip | ColliderTypeBall); body->setContactTestBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBall); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); this->bird->setPhysicsBody(body); this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5); this->bird->idle(); this->addChild(this->bird); /* //初始化加速粒子 accelerateParticle = ParticleSystemQuad::create("particleImpact.plist"); accelerateParticle->setScale(0.5f); accelerateParticle->setPosition(0,0); addChild(accelerateParticle); */ accelerateParticle = NULL; pipSpeed = 2; /* //闪亮 Blink *blink = Blink::create(5.0f, 10); bird->runAction(blink); */ //Ball this->ball = Sprite::create("ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBall); ballbody->setCollisionBitmask(ColliderTypePip | ColliderTypeLand); ballbody->setContactTestBitmask(ColliderTypePip | ColliderTypeLand); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(false); ball->setPhysicsBody(ballbody); // static Shaky3D* create(float duration, const Size& gridSize, int range, bool shakeZ); ball->setPosition(bird->getPositionX(),bird->getPositionY()+30); ball->setTag(100); // addChild(ball); BallisTouch = false; //BallName ballName = Sprite::create("BallWithHoney.png"); ballName->setPosition(ball->getPositionX(),ball->getPositionY()+40); //addChild(ballName); // Add the ground this->groundNode = Node::create(); float landHeight = BackgroundLayer::getLandHeight(); auto groundBody = PhysicsBody::create(); groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setCategoryBitmask(ColliderTypeLand); groundBody->setCollisionBitmask(ColliderTypeBird | ColliderTypeBall); groundBody->setContactTestBitmask(ColliderTypeBird | ColliderTypeLand); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(144, landHeight/2); this->addChild(this->groundNode); // init land this->landSpite1 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite1->setAnchorPoint(Point::ZERO); this->landSpite1->setPosition(Point::ZERO); this->addChild(this->landSpite1, 30); this->landSpite2 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite2->setAnchorPoint(Point::ZERO); this->landSpite2->setPosition(this->landSpite1->getContentSize().width-2.0f,0); this->addChild(this->landSpite2, 30); shiftLand = schedule_selector(GameLayer::scrollLand); this->schedule(shiftLand, 0.01f); this->scheduleUpdate(); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }else { return false; } }
bool LevelFifteen::init() { if (!Layer::init()) { return false; } // 用于解决刚体穿透问题 this->scheduleUpdate(); // 计时器 this->schedule(schedule_selector(LevelFifteen::timeCounter), 1.0f); auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); // 加载背景贴图 auto spriteBg = Sprite::create("background.png"); spriteBg->setAnchorPoint(Vec2::ZERO); spriteBg->setPosition(Vec2::ZERO); addChild(spriteBg); // 将资源加载到精灵帧缓存 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("threeColor.plist"); ballOne = Sprite::createWithSpriteFrameName("hero.png"); ballOne->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f); auto ballBodyOne = PhysicsBody::createCircle(ballOne->getContentSize().width / 2, PHYSICSBODY_MATERIAL_DEFAULT); //是否设置物理为静态 //ballBodyOne->setDynamic(false); //设置物理的恢复力 ballBodyOne->getShape(0)->setRestitution(0.5f); //设置物体的摩擦力 ballBodyOne->getShape(0)->setFriction(0.0f); ballBodyOne->getShape(0)->setDensity(0.3f); // 设置质量 质量等于密度乘以面积 //ballBodyOne->getShape(0)->setMass(5000); // 设置物体是否受重力系数影响 ballBodyOne->setGravityEnable(true); ballBodyOne->setCategoryBitmask(1);// 分类掩码 ballBodyOne->setCollisionBitmask(1 | 2 | 4 | 8);// 碰撞掩码 ballBodyOne->setContactTestBitmask(2);// 接触测试掩码 // 把物体添加到精灵 ballOne->setPhysicsBody(ballBodyOne); this->addChild(ballOne); // 创建抢食者(黑色) Sprite* black = nullptr; PhysicsBody* blackBody = nullptr; Vec2 arrBlack[] = { Vec2(-34.00000, -32.50000), Vec2(-15.00000, 36.00000), Vec2(35.00000, -15.00000) }; int indexBlack = CCRANDOM_0_1() * 3; switch (indexBlack) { case 0: { black = Sprite::createWithSpriteFrameName("black1.png"); blackBody = PhysicsBody::createBox(black->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); } break; case 1: { black = Sprite::createWithSpriteFrameName("black2.png"); blackBody = PhysicsBody::createCircle(black->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); } break; case 2: { black = Sprite::createWithSpriteFrameName("black3.png"); blackBody = PhysicsBody::createPolygon(arrBlack, 3, PHYSICSBODY_MATERIAL_DEFAULT); } break; default: break; } black->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f); blackBody->setDynamic(true); blackBody->setGravityEnable(false); blackBody->setCategoryBitmask(8); blackBody->setCollisionBitmask(1 | 2 | 4 | 8); blackBody->setContactTestBitmask(2); black->setPhysicsBody(blackBody); this->addChild(black); //创建一个盒子,用来碰撞 auto edgeSpace = Sprite::create(); auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); boundBody->getShape(0)->setFriction(0.0f); boundBody->getShape(0)->setRestitution(1.0f); edgeSpace->setPhysicsBody(boundBody); edgeSpace->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(edgeSpace); edgeSpace->setTag(0); boundBody->setCategoryBitmask(4); boundBody->setCollisionBitmask(1 | 2 | 4 | 8); boundBody->setContactTestBitmask(0); // 创建yuan auto white1 = Sprite::createWithSpriteFrameName("white2.png"); auto whiteBody1 = PhysicsBody::createCircle(white1->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); whiteBody1->setDynamic(false); whiteBody1->setCategoryBitmask(4); whiteBody1->setCollisionBitmask(1 | 2 | 4 | 8); whiteBody1->setContactTestBitmask(0); white1->setPhysicsBody(whiteBody1); this->addChild(white1); // 正方形 auto white1_2 = Sprite::createWithSpriteFrameName("white1.png"); auto whiteBody1_2 = PhysicsBody::createBox(white1_2->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); whiteBody1_2->setDynamic(false); whiteBody1_2->setCategoryBitmask(4); whiteBody1_2->setCollisionBitmask(1 | 2 | 4 | 8); whiteBody1_2->setContactTestBitmask(0); white1_2->setPhysicsBody(whiteBody1_2); this->addChild(white1_2); // 创建三角形 Vec2 arr[] = { Vec2(-16.50000, 36.00000), Vec2(35.50000, -15.00000), Vec2(-34.00000, -32.50000) }; auto white3 = Sprite::createWithSpriteFrameName("white3.png"); auto whiteBody3 = PhysicsBody::createPolygon(arr, 3, PHYSICSBODY_MATERIAL_DEFAULT); whiteBody3->setDynamic(false); whiteBody3->setCategoryBitmask(4); whiteBody3->setCollisionBitmask(1 | 2 | 4 | 8); whiteBody3->setContactTestBitmask(0); white3->setPhysicsBody(whiteBody3); this->addChild(white3); // 随机位置 int index = CCRANDOM_0_1() * 3; switch (index) { case 0: { white1->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 200.0f); white1_2->setPosition(visibleSize.width / 2.0f + 200.0f, visibleSize.height / 2.0f); white3->setPosition(visibleSize.width / 2.0f - 200.0f, visibleSize.height / 2.0f); } break; case 1: { white1_2->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 200.0f); white1->setPosition(visibleSize.width / 2.0f + 200.0f, visibleSize.height / 2.0f); white3->setPosition(visibleSize.width / 2.0f - 200.0f, visibleSize.height / 2.0f); } break; case 2: { white3->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 200.0f); white1->setPosition(visibleSize.width / 2.0f + 200.0f, visibleSize.height / 2.0f); white1_2->setPosition(visibleSize.width / 2.0f - 200.0f, visibleSize.height / 2.0f); } break; default: break; } // 正方形旋转 auto rotation = RotateBy::create(1, 360); auto rotationback = rotation->reverse(); auto seq = Sequence::create(rotation, rotationback, nullptr); auto repeat = RepeatForever::create(seq); white1->runAction(repeat); // // 第二个正方形旋转 auto rotation_1 = RotateBy::create(2, 360); auto rotationback_1 = rotation_1->reverse(); auto seq1 = Sequence::create(rotation_1, rotationback_1, nullptr); auto repeat1 = RepeatForever::create(seq1); white1_2->runAction(repeat1); // 三角形旋转 auto rotation_2 = RotateBy::create(2, 360); auto rotationback_2 = rotation_2->reverse(); auto seq2 = Sequence::create(rotation_2, rotationback_2, nullptr); auto repeat2 = RepeatForever::create(seq2); white3->runAction(repeat2); auto yuantong0 = Sprite::createWithSpriteFrameName("yuantong.png"); yuantong0->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height + yuantong0->getContentSize().height / 2.0f)); auto yuantongBody = PhysicsBody::createBox(yuantong0->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); yuantongBody->setDynamic(false); yuantongBody->setCategoryBitmask(4); yuantongBody->setCollisionBitmask(1 | 2 | 4 | 8); yuantongBody->setContactTestBitmask(0); yuantong0->setPhysicsBody(yuantongBody); this->addChild(yuantong0); auto moveby0 = MoveBy::create(1.0f, Vec2(0, -90)); auto moveby0back = moveby0->reverse(); auto easein = EaseBackIn::create(moveby0); auto seq0 = Sequence::create(easein, CallFuncN::create(CC_CALLBACK_1(LevelFifteen::callbackC, this)), moveby0back, nullptr); auto repeat0 = RepeatForever::create(seq0); yuantong0->runAction(repeat0); listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [=](Touch* touch, Event* event){ auto touchPosition = touch->getLocation(); Vec2 force = Vec2::ZERO; if ((touchPosition.x > visibleSize.width / 2.0f) && (touchPosition.y < visibleSize.height / 2.0f)) { force = Vec2(50000.0f, 0.0f); } else if ((touchPosition.y < visibleSize.height / 2.0f) && (touchPosition.x < visibleSize.width / 2.0f)) { force = Vec2(-50000.0f, 0.0f); } else { force = Vec2(0.0f, 50000.0f); } ballOne->getPhysicsBody()->applyImpulse(force); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // 时间显示文本 test = LabelAtlas::create(StringUtils::toString(nGoal), "1.png", 14, 21, '0'); test->setScale(2.0f); test->setAnchorPoint(Vec2::ANCHOR_MIDDLE); test->setPosition(Vec2(test->getContentSize().width + 10, visibleSize.height - test->getContentSize().height)); this->addChild(test); test2 = LabelAtlas::create(StringUtils::toString(newTimes), "1.png", 14, 21, '0'); test2->setScale(2.0f); test2->setAnchorPoint(Vec2::ANCHOR_MIDDLE); test2->setPosition(Vec2(visibleSize.width - test2->getContentSize().width - 10, visibleSize.height - test2->getContentSize().height)); this->addChild(test2); test3 = LabelAtlas::create(StringUtils::toString(nTimes), "1.png", 14, 21, '0'); test3->setScale(2.0f); test3->setAnchorPoint(Vec2::ANCHOR_MIDDLE); test3->setPosition(Vec2(test3->getContentSize().width*4.0f + 40, visibleSize.height - test3->getContentSize().height)); this->addChild(test3); return true; }
bool GameScene::init() { if (!Layer::init()) { return false; } Size winSize =Director::getInstance()->getWinSize(); ParticleSystemQuad* qual = ParticleSystemQuad::create("res/effect/dots_birth.plist"); ParticleBatchNode* bNode = ParticleBatchNode::createWithTexture(qual->getTexture()); bNode->addChild(qual); this->addChild(bNode); qual->setPosition(Vec2(320,568)); auto spawnHero = CallFunc::create([=]() { m_hero = Hero::create(); this->addChild(m_hero); addJointForHero(); bNode->removeFromParent(); }); auto spawnMobs = CallFunc::create([=]() { Enemy* enemy = Enemy::create(); enemy->setPosition(Vec2(400,200)); addChild(enemy); enemy->moveToHero(m_hero->getCurMovingNode()->getPosition()); }); auto spawnHeroSequ = Sequence::create(DelayTime::create(0.8),spawnHero,spawnMobs,NULL); runAction(spawnHeroSequ); EventListenerTouchOneByOne* listenner = EventListenerTouchOneByOne::create(); listenner->onTouchBegan = [=](Touch* touch,Event* evt) { m_hero->swapCircle(); Vec2 pos = m_hero->getCurMovingNode()->getPosition(); if( shoudMoveCameraPos(pos) ) m_camera->runAction(MoveTo::create(0.3,pos)); // m_hero->changeState(); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listenner,this); Sprite* bg = Sprite::create("res/bg_1.png"); addChild(bg,-1); bg->setPosition(winSize/2); bg->setScale(2,3); auto edge = Node::create(); edge->setPosition(winSize/2); PhysicsBody* edgeBody = PhysicsBody::createEdgeBox(winSize*1.3,PhysicsMaterial(PHYSICS_INFINITY,0,0),10,Vec2(0,0)); edge->setPhysicsBody(edgeBody); edgeBody->setDynamic(true); addChild(edge); edgeBody->setCategoryBitmask(0xFFFFFFFF); edgeBody->setCollisionBitmask(0xFFFFFFFF); edgeBody->setContactTestBitmask(0X00001000); EventListenerPhysicsContact* physicsListenner = EventListenerPhysicsContact::create(); physicsListenner->onContactBegin=[=](PhysicsContact& contact) { //英雄和墙发生碰撞 if ((contact.getShapeA()->getContactTestBitmask()|contact.getShapeB()->getContactTestBitmask()) == 0x11001000) { m_hero->reverse(); } //英雄和食物发生碰撞 else if ((contact.getShapeA()->getContactTestBitmask()|contact.getShapeB()->getContactTestBitmask()) == 0x11000100) { exchangeSide(); if ((contact.getShapeA()->getContactTestBitmask()) == 0x00000100) { contact.getShapeA()->getBody()->getNode()->setVisible(false); } else if((contact.getShapeB()->getContactTestBitmask()) == 0x00000100) { contact.getShapeB()->getBody()->getNode()->setVisible(false); } } //英雄和怪物发生碰撞 else if ((contact.getShapeA()->getContactTestBitmask()|contact.getShapeB()->getContactTestBitmask())== 0x11000110) { Vec2 contactPos = contact.getContactData()->points[0]; ParticleSystemQuad* quad = ParticleSystemQuad::create("res/effect/die.plist"); quad->setPosition(contactPos); ParticleBatchNode* batch = ParticleBatchNode::createWithTexture(quad->getTexture()); batch->addChild(quad); addChild(batch); auto removeFunc = CallFunc::create([=](){ batch->removeFromParent(); }); Sequence* sequ = Sequence::create(DelayTime::create(0.8f),removeFunc,NULL); runAction(sequ); } return true; }; physicsListenner->onContactPreSolve = [=](PhysicsContact& contact,PhysicsContactPreSolve& preSlove) { CCLOG("PreSlove"); return false; }; physicsListenner->onContactPostSolve = [=](PhysicsContact& contact,const PhysicsContactPostSolve& postSlove) { CCLOG("PostSlove"); }; physicsListenner->onContactSeperate = [=](PhysicsContact& contact) { if ((contact.getShapeA()->getContactTestBitmask()) == 0x00000100) { contact.getShapeA()->getBody()->getNode()->removeFromParent(); } else if((contact.getShapeB()->getContactTestBitmask()) == 0x00000100) { contact.getShapeB()->getBody()->getNode()->removeFromParent(); } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(physicsListenner,this); m_camera = Camera::create(); m_camera->setPosition(winSize/2); addChild(m_camera); Enhance* en =Enhance::create(); en->setPosition(Vec2(100,100)); addChild(en); 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; }
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; }