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*******************/ }
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 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); } }
bool HeroSprite::init() { bool bRet = false; do { CC_BREAK_IF(!Sprite::initWithSpriteFrameName("heroStand_0001.png")); Size heroSize = this->getContentSize(); PhysicsBody *body = PhysicsBody::createBox(heroSize-Size(0,16), PhysicsMaterial(0, 0, 0)); body->setLinearDamping(0.0f); body->setDynamic(true); body->setGravityEnable(true); body->setTag(TAG_HERO_PHYS_BODY); body->setCategoryBitmask(1<<2); body->setContactTestBitmask(1<<0 | 1<<1); body->setMass(50); body->setRotationEnable(false); this->setPhysicsBody(body); mState = STATE_IDLE; mRunAnimate = NULL; mSmokeRunAnimate = NULL; bRet = true; } while(0); return bRet; }
bool 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 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; }
void Ball::initPhysics(){ PhysicsBody *spriteBody = PhysicsBody::createCircle(this->getSprite()->getBoundingBox().size.width , PHYSICSBODY_MATERIAL_DEFAULT); spriteBody->setDynamic(true); spriteBody->getShape(0)->setRestitution(1.0f); spriteBody->getShape(0)->setFriction(0.0f); spriteBody->getShape(0)->setDensity(1.0f); spriteBody->getShape(0)->setMass(100); spriteBody->setGravityEnable(false); spriteBody->setVelocity(this->velocity); this->getSprite()->setPhysicsBody(spriteBody); }
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; }
Chopper* Chopper::createKnife(float v, float y) { Chopper* knife = Chopper::create(); knife->retain(); knife->chopper = Sprite::createWithSpriteFrameName("knife4.png"); knife->chopper->setScale(0.6); PhysicsBody *body = PhysicsBody::create(); body->setGravityEnable(true); //body->setGravityEnable(false); body->setVelocity(Vec2(0, -v)); knife->chopper->setPhysicsBody(body); knife->chopper->setPosition(Vec2(410, y)); return knife; }
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); }
void BlockBase::initPhysics() { auto size = Size(mSprite->getScaleX() * mImageSize, mSprite->getScaleY() * mImageSize); PhysicsBody* pbody = nullptr; if(mKind != KIND_DEATH_CIRCLE && mKind != KIND_FORCEFIELD) pbody = PhysicsBody::createBox(size); else pbody = PhysicsBody::createCircle(std::max(size.height/2, size.width/2)); pbody->setDynamic((mKind == KIND_PUSHABLE || mKind == KIND_HERO) ? true : false); pbody->setContactTestBitmask(1); pbody->setGravityEnable(false); mEnableGravity = mKind == KIND_PUSHABLE || mKind == KIND_HERO; mSprite->setPhysicsBody(pbody); }
bool Witch::init() { mWitch = CCSprite::createWithSpriteFrameName(Tex::Player_0); this->addChild(mWitch); CCLOG("Witch Width:%f", mWitch->getContentSize().width); mWitch->setPosition(0, 0); PhysicsBody *body = PhysicsBody::create(); body->setTag(1); this->setPhysicsBody(body); body->addShape(PhysicsShapeBox::create(Size(mWitch->getContentSize().width, mWitch->getContentSize().height))); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); return true; }
BoundWall::BoundWall(WallType type, Size screenSize) { PhysicsBody* body = nullptr; switch (type) { case UP: this->setAnchorPoint(Vec2(0.5f, 0)); this->setPosition(Vec2(screenSize.width / 2, screenSize.height)); body = PhysicsBody::createBox(Size(screenSize.width,5), PhysicsMaterial(1, 0, 0)); break; case DOWN: this->setAnchorPoint(Vec2(0.5f, 1)); this->setPosition(Vec2(screenSize.width / 2, 0)); body = PhysicsBody::createBox(Size(screenSize.width, 5), PhysicsMaterial(1, 0, 0)); break; case LEFT: this->setAnchorPoint(Vec2(1, 0.5f)); this->setPosition(Vec2(0, screenSize.height / 2)); body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0)); break; case RIGHT: this->setAnchorPoint(Vec2(0, 0.5f)); this->setPosition(Vec2(screenSize.width, screenSize.height / 2)); body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0)); break; default: break; } body->setGravityEnable(false); body->setDynamic(false); body->setTag(Tags::GROUND); body->setCollisionBitmask(true); body->setContactTestBitmask(true); this->setPhysicsBody(body); }
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); }
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 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; }
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()) return false; // Welcome Message Size visiableSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // screen background auto screenBackground = Sprite::create("screen_background.png"); screenBackground->setAnchorPoint(Point::ZERO); screenBackground->setPosition(Vec2(0, 0)); this->addChild(screenBackground); // sprite ⇨ mosquito mosquito = Sprite::create("mosquito.png"); mosquito->setPosition(Vec2(30, visiableSize.height - 300)); mosquito->setScale(0.1 * ( visiableSize.width / mosquito->getBoundingBox().size.width)); this->addChild(mosquito); //bug->runAction(this->create(6, bug->getPosition(), Vec2(visiableSize.width - 30, visiableSize.height - 300), 100)); mosquito->runAction(Spawn::create(RotateBy::create(5, 1080), this->create(5, mosquito->getPosition(),Vec2(visiableSize.width - 30, visiableSize.height - 300), 100), NULL)); PhysicsBody *body = PhysicsBody::createCircle(BUG_RADIUS); body->addShape(PhysicsShapeCircle::create(BUG_RADIUS)); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); body->setMass(1.0f); body->setContactTestBitmask(0xFFFFFFFF); this->mosquito->setPhysicsBody(body); // ************************************************************************ // // MotionStreakを作成 pStreak = MotionStreak::create(1.0, 1.0f, 3.0f, Color3B::GREEN, "suspend.png"); addChild(pStreak); this->groundNode = Node::create(); //イベントリスナー作成 auto listener = EventListenerTouchOneByOne::create(); //タッチ開始時 listener->onTouchBegan = [this](Touch* touch, Event* event) { Point pos = this->convertTouchToNodeSpace(touch); this->pStreak->setPosition(pos); auto groundBody = PhysicsBody::createBox(Size(4, pos.y)); groundBody->addShape(PhysicsShapeBox::create(Size(4, pos.y))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setMass(1.0f); groundBody->setContactTestBitmask(0xFFFFFFFF); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(pos.x, pos.y); this->addChild(this->groundNode); return true; }; //タッチ移動時 listener->onTouchMoved = [this](Touch* touch, Event* event) { Point pos = this->convertTouchToNodeSpace(touch); this->pStreak->setPosition(pos); }; //ディスパッチャに登録 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 100); // *************************************************************************************** // // sprite ⇨ suspend (init for invisible) suspendBackground = Sprite::create("suspend_background.png"); suspendBackground->setAnchorPoint(Point::ZERO); suspendBackground->setPosition(Vec2(0, 0 + 40)); suspendBackground->setScale(visiableSize.width / suspendBackground->getBoundingBox().size.width, (visiableSize.height - 40)/ suspendBackground->getBoundingBox().size.height); suspendBackground->setTag(2009); suspendBackground->setVisible(false); this->addChild(suspendBackground); //进入暂停 auto suspendBtn = Sprite::create("suspend.png"); auto menuSuspendItem = MenuItemSprite::create(suspendBtn, suspendBtn, NULL, CC_CALLBACK_1(GameLayer::menuSuspendCallback, this)); menuSuspendItem->setPosition(Vec2(visiableSize.width - suspendBtn->getBoundingBox().size.width - 20, visiableSize.height - suspendBtn->getBoundingBox().size.height - 20)); auto menu = Menu::create(menuSuspendItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 3); // suspend close button auto closeButton = Sprite::create("exit.png"); MenuItem *menuCloseItem = MenuItemSprite::create(closeButton, closeButton, NULL, CC_CALLBACK_1(GameLayer::menuSuspendCloseCallback, this)); menuCloseItem->setPosition(Vec2(245, 128)); pauseMenu = Menu::create( menuCloseItem, NULL); pauseMenu->setPosition(Point::ZERO); pauseMenu->setVisible(false); this->addChild(pauseMenu, 4); schedule(schedule_selector(GameLayer::update), 2.0f); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); 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 | 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 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; }
/* Khoi tao game border */ void PlayGame::createGameBorder() { //=================================================================================== //=================================================================================== // CREATE BORDER _border = Node::create(); _border->setTag(BORDER_TAG); // Tao body cho khung vat ly PhysicsMaterial theo thu tu (Density , Restitution , Friction) // Density : ty trong, mat do , Restitution : Dan hoi , Friction : ma sat PhysicsBody* borderBody = PhysicsBody::createEdgeBox(_visibleSize - Size( WIDTH_OFFSET , HEIGHT_OFFSET ) * 2 , PhysicsMaterial(1.0f, 1.0f, 0.0f)); borderBody->setGravityEnable(false); borderBody->setContactTestBitmask(eObjectBitMask::BORDER); _border->setPhysicsBody(borderBody); _border->setPosition(Vec2(_visibleSize.width/2 , _visibleSize.height/2)); this->addChild(_border); //=================================================================================== //=================================================================================== // CREATE WALL1 _wallDie1 = Node::create(); _wallDie1->setAnchorPoint(Vec2::ANCHOR_MIDDLE); _wallDie1->setTag(WALL_DIE_TAG_1); Vec2 vec[2] = { Vec2(0 , 0) , Vec2(_visibleSize.width - WIDTH_OFFSET * 2 , 0) }; PhysicsBody* wallBody1 = PhysicsBody::createEdgeChain(vec , 2 ,PhysicsMaterial(1.0f, 1.0f, 0.0f)); wallBody1->setGravityEnable(false); wallBody1->setDynamic(true); wallBody1->setRotationEnable(false); wallBody1->setContactTestBitmask(eObjectBitMask::WALL_DIE); _wallDie1->setPhysicsBody(wallBody1); _wallDie1->setPosition(Vec2( WIDTH_OFFSET , HEIGHT_OFFSET + 3)); this->addChild(_wallDie1); //=================================================================================== //=================================================================================== // CREATE WALL2 _wallDie2 = Node::create(); _wallDie2->setAnchorPoint(Vec2::ANCHOR_MIDDLE); _wallDie2->setTag(WALL_DIE_TAG_2); PhysicsBody* wallBody2 = PhysicsBody::createEdgeChain(vec , 2 , PhysicsMaterial(1.0f, 1.0f, 0.0f)); wallBody2->setGravityEnable(false); wallBody2->setDynamic(true); wallBody2->setRotationEnable(false); wallBody2->setContactTestBitmask(eObjectBitMask::WALL_DIE); _wallDie2->setPhysicsBody(wallBody2); _wallDie2->setPosition(Vec2(WIDTH_OFFSET , _visibleSize.height - HEIGHT_OFFSET - 3)); this->addChild(_wallDie2); }