void Devil::InitSprite() { auto body = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(100, 1, 1)); body->setDynamic(true); body->setGravityEnable(false); body->setRotationEnable(false); body->setCollisionBitmask(MONSTER_BODY_COLLISION_BITMASK); body->setContactTestBitmask(true); this->setPhysicsBody(body); this->setUserData(this); hpBgSpr = Sprite::create("Monster/hpBg.png"); this->addChild(hpBgSpr, ZINDEX_MONSTER_HP); hpCurrentSpr = Sprite::create("Monster/hpCurrent.png"); this->addChild(hpCurrentSpr, ZINDEX_MONSTER_HP); auto origin = Director::getInstance()->getVisibleOrigin(); hpBgSpr->setPosition(Vec2(this->getContentSize().width / 2, this->getPosition().y + this->getContentSize().height + hpBgSpr->getContentSize().height / 2)); hpCurrentSpr->setPosition(hpBgSpr->getPosition()); hpBgSpr->setOpacity(0); hpCurrentSpr->setOpacity(0); fury = CSLoader::createNode("Effect/Rage/Rage.csb"); fury->setPosition(this->getContentSize().width * 0.5f, this->getContentSize().height * 0.5f); this->addChild(fury); fury->setVisible(false); auto visibleSize = Director::getInstance()->getVisibleSize(); d = Dragon::create(layer); d->setPosition(visibleSize.width*0.7f, visibleSize.height*0.7f); layer->addChild(d, ZINDEX_MONSTER_SPRITE); d->scheduleUpdate(); }
bool Player::init() { if (!Sprite::initWithFile("graphics/luk_sprite.png")) { return false; } this->setAnchorPoint(Vec2::ANCHOR_MIDDLE); cocos2d::Point PBox[8]{cocos2d::Point(-2, -12), cocos2d::Point(-4, -10), cocos2d::Point(-4, -2), cocos2d::Point(-2, 0), cocos2d::Point(2, 0), cocos2d::Point(4, -2), cocos2d::Point(4, -10), cocos2d::Point(2, -12)}; playerRect = Rect(); auto body = PhysicsBody::createPolygon(PBox, 8); body->setEnable(true); body->setDynamic(true); body->setGravityEnable(true); body->setRotationEnable(false); body->setVelocityLimit(30.0); body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); //BLOCKSとの接触判定をON body->setCollisionBitmask(static_cast<int>(Stage::TileType::EMPTY)); body->setContactTestBitmask(static_cast<int>(Stage::TileType::BLOCKS)); //body->setContactTestBitmask(INT_MAX); this->setPhysicsBody(body); this->scheduleUpdate(); return true; }
Bird::Bird(cocos2d::Layer *layer){ visibleSize = Director::getInstance()->getVisibleSize(); origin = Director::getInstance()->getVisibleOrigin(); flappyBird = Sprite::create("Will.png"); flappyBird->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); willLocationX = flappyBird->getPositionX(); willLocationY = flappyBird->getPositionY(); auto flappyBody = PhysicsBody::createBox(Size(flappyBird->getContentSize().width, flappyBird->getContentSize().height), PhysicsMaterial(0, 1, 0)); flappyBody->setGravityEnable(false); flappyBird->setPhysicsBody(flappyBody); layer -> addChild(flappyBird, 100); flappyBody->setCollisionBitmask(BIRD_COLLISION_BITMASK); flappyBody->setContactTestBitmask(true); flappyBody->setMass(1); //isFalling = true; flappyBird->getPhysicsBody()->setVelocityLimit(300); }
void BreakoutMainScene::createBall() { // Create Ball cocos2d::log("Create Ball"); // Tao qua bong va thiet lap khung vat ly cho no ball = Sprite::create("breakout_img/breakout_ball.png"); ball->setAnchorPoint(Vec2::ANCHOR_MIDDLE); ball->setPosition(Vec2(WINSIZE.width/2 , WINSIZE.height/2 + 50)); auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width/2 , PHYSICSBODY_MATERIAL_DEFAULT); ballBody->getShape(0)->setDensity(1.0f); // trong luc ballBody->getShape(0)->setFriction(0.0f); ballBody->getShape(0)->setRestitution(1.0f); ballBody->setDynamic(true); ballBody->setContactTestBitmask(0x00000001); ballBody->setGravityEnable(false); // Khong set gia toc // Tao 1 luc tac dong Vect force = Vect(900000.0f, -900000.0f); ballBody->applyImpulse(force); ball->setPhysicsBody(ballBody); ball->setTag(Tag::T_Ball); this->addChild(ball); }
bool Player::init() { if (!Sprite::initWithFile("graphics/luk_sprite.png")) { return false; } //Point PBox[8]{Point(-2, -12), Point(-4, -10), Point(-4, -2), Point(-2, 0), Point(2, 0), Point(4, -2), Point(4, -10), Point(2, -12)}; Point PBox[4]{Point(-4, -12), Point(-4, 0), Point(4, 0), Point(4, -12)}; //auto body = PhysicsBody::createEdgePolygon(PBox, 4,PHYSICSBODY_MATERIAL_DEFAULT,0.5f); auto body = PhysicsBody::createPolygon(PBox, 4); body->setEnable(true); body->setDynamic(true); body->setGravityEnable(true); body->setRotationEnable(false); body->setVelocityLimit(30.0); body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); //BLOCKSとの接触判定をON //body->setCollisionBitmask(static_cast<int>(Stage::TileType::BLOCKS)); body->setCollisionBitmask(static_cast<int>(Stage::TileType::NONE)); body->setContactTestBitmask(static_cast<int>(Stage::TileType::BLOCKS)); //body->setContactTestBitmask(INT_MAX); this->setPhysicsBody(body); this->setAnchorPoint(Vec2::ANCHOR_MIDDLE); this->scheduleUpdate(); return true; }
bool Bullet::init (int tag) { // if (!Sprite::initWithFile("faceball.png")){ if (!Sprite::initWithFile("ball.png")){ return false; } m_tag = tag; this->setColor(_tagColor[tag]); // 大きさをランダムにとりあえず bulletSize = _bulSize[tag]; // this->setContentSize(cocos2d::Size (bulletSize * 20, bulletSize * 20)); // もともと設定されてたもの // なんか、物理エンジンがおかしくなるような気がする // これを消すと落ち着く this->setScale(0.13f * bulletSize); //this->setScale(0.043f * bulletSize); // 密度、反発、摩擦 // まるいやつ。回転がわかりズラい // auto pBall = PhysicsBody::createCircle(bulletSize, // PhysicsMaterial(10.0f, 0.6f, 0.3f)); // しかくいやつ。これは四角すぎる // auto pBall = PhysicsBody::createBox(cocos2d::Size(bulletSize, bulletSize), PhysicsMaterial(1.0f, 0.6f, 0.3f)); Point spritePoints[12] = { Point(-1, 0.333) *= bulletSize, Point(-0.666, 0.5) *= bulletSize, Point(-0.333, 0.666) *= bulletSize, Point(0.333, 0.666) *= bulletSize, Point(0.666, 0.5) *= bulletSize, Point(1, 0.333) *= bulletSize, Point(1, -0.333) *= bulletSize, Point(0.666, -0.5) *= bulletSize, Point(0.333, -0.666) *= bulletSize, Point(-0.333, -0.666) *= bulletSize, Point(-0.666, -0.5) *= bulletSize, Point(-1, -0.333) *= bulletSize }; // for (Point target : spritePoints) { // target *= bulletSize; // } // 密度、反発、摩擦 auto pBall = PhysicsBody::createPolygon(spritePoints, 12, PhysicsMaterial(1.0f, 0.0f, 1.0f)); // auto pBall = PhysicsBody::createPolygon(spritePoints, 12, PhysicsMaterial(1.0f, 0.6f, 0.3f)); pBall->setDynamic(true); pBall->setRotationEnable(true); // 回転率?なのか?これを無限にすると回転しない // pBall->setMoment(PHYSICS_INFINITY); // pBall->setMoment(bulletSize); pBall->setGravityEnable(true); //log("%f",pBall->getMass()); // pBall->setMass(1.0); pBall->setVelocityLimit(800); this->setPhysicsBody(pBall); return true; }
void ExplisonScreen::initBody() { auto body = PhysicsBody::createBox(m_WinSize); body->setGravityEnable(false); body->setMass(100000); body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK); body->setCollisionBitmask(0); body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK); setPhysicsBody(body); }
void Player::SetPlayer(cocos2d::Layer *layer) { CCLOG("POWER"); //COCOS BUG: WONT ALWAYS SHOW IN OUTPUT paddle = Sprite::create("Paddle.png"); // May not need the auto auto paddleBounding = PhysicsBody::createBox(paddle->getContentSize());// CHANGE TO CIRCLE PhysicsShapeCircle paddleBounding->setDynamic(false); paddleBounding->setGravityEnable(false); paddleBounding->setContactTestBitmask(Paddle_Bitmask); paddleBounding->setCollisionBitmask(Paddle_Bitmask); paddle->setPhysicsBody(paddleBounding); //sets a bounding box around brick. SetPlayerPos(WinSize.width / 2, WinSize.height / 6); layer->addChild(paddle,1); paddle->setTag(1); }
void PlaneProtect::initBody() { auto body = PhysicsBody::createCircle(getContentSize().width * 0.5); body->setGravityEnable(false); body->setMass(10000000); body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK); body->setCollisionBitmask(0); body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK); setPhysicsBody(body); setMaxBlood(Protect_Hp); m_ContactPro.hurts = -Ene_Plane_Hp_max - 10; setUnitId(UnitId::eFanghudun); }
void GameScene::addPlayer(TMXTiledMap *tmx){ auto physicsBody = PhysicsBody::createBox(Size(63, 155), PhysicsMaterial(0, 0, 0)); physicsBody->setGravityEnable(false); Sprite *player = Sprite::create("flying1.png"); player->setPhysicsBody(physicsBody); mPlayer = Player::create(); mPlayer->setPosition(Point(-33, 180)); mPlayer->bindSprite(player); this->addChild(mPlayer); mPlayer->addAnimationCache(); }
void PlayLayer::startPlay() { Size visibleSize = Director::getInstance()->getVisibleSize(); edgeSp = Sprite::create(); auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); boundBody->getShape(0)->setRestitution(1.0f); boundBody->getShape(0)->setFriction(0.0f); boundBody->getShape(0)->setDensity(1.0f); edgeSp->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2)); edgeSp->setPhysicsBody(boundBody); this->addChild(edgeSp); edgeSp->setTag(0); ball = Sprite::create("CloseNormal.png", Rect(0, 0, 52, 52)); ball->setPosition(100, 100); auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width / 2.); ballBody->getShape(0)->setRestitution(1.0f); ballBody->getShape(0)->setFriction(0.0f); ballBody->getShape(0)->setDensity(1.0f); ballBody->setGravityEnable(false); ball->setPhysicsBody(ballBody); ball->setTag(1); this->addChild(ball); paddle = Sprite::create("paddle.png"); auto paddleBody = PhysicsBody::createBox(paddle->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); paddleBody->getShape(0)->setRestitution(1.0f); paddleBody->getShape(0)->setFriction(0.0f); paddleBody->getShape(0)->setDensity(10.0f); paddleBody->setGravityEnable(false); paddleBody->setDynamic(false); paddle->setPosition(visibleSize.width / 2, 50); paddle->setPhysicsBody(paddleBody); ball->setTag(2); this->addChild(paddle); }
Enemy::Enemy(int iD, Object_T oT, Enemy_T et, float x, float y, string bod, string attk, int hp, int dmg, bool ded) :Type(et), body_fn(bod), attack_fn(attk), attack_dmg(dmg), dead(ded) { ID = iD; ot = oT; body=Sprite::create(body_fn); body->setPosition(x,y); auto physicsBody = cocos2d::PhysicsBody::createBox(getBody()->getContentSize(), cocos2d::PhysicsMaterial(0,1,0)); physicsBody->setCollisionBitmask(ID); physicsBody->setCategoryBitmask(1); physicsBody->setContactTestBitmask(true); physicsBody->setGravityEnable(false); physicsBody->setDynamic(true); body->setPhysicsBody(physicsBody); health = hp; }
Scene* HelloWorld::createScene() { auto scene = Scene::createWithPhysics(); //调试 scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); scene->getPhysicsWorld()->setGravity(Vec2(0.0f, -400.0f)); //设置重力加速度、默认为100 auto layer = HelloWorld::create(); layer->setphyworld(scene->getPhysicsWorld()); //将创建的物理世界传递到子层中 Size visibleSize=Director::getInstance()->getVisibleSize(); //线宽为1 auto body=PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,1); body->setGravityEnable(true); auto edgeNode=Node::create(); edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); edgeNode->setPhysicsBody(body); scene->addChild(edgeNode); scene->addChild(layer); return scene; }
bool Prop::init() { if (!BaseAirObject::initWithFile("propBanShou.png")) { return false; } auto vertexes = PhysicsHelper::getInstance()->getPhysicsShapeByName("propBanShou.plist"); auto body = PhysicsBody::create(); for (auto shapeVex : vertexes) { auto shape = PhysicsShapePolygon::create(shapeVex.data(), shapeVex.size()); body->addShape(shape); } body->setGravityEnable(false); body->setDynamic(false); body->setContactTestBitmask(1); this->setPhysicsBody(body); return true; }
void Wall::initBody(const Size& size,const PhysicsMaterial& pm ) { auto body = PhysicsBody::createBox(size,pm,Point::ZERO); setPhysicsBody(body); body->setGravityEnable(false); body->setMass(100000); body->setDynamic(false); body->setCategoryBitmask(WALL_CATEGORYBITMASK); body->setCollisionBitmask(WALL_COLLISIONBITMASK); body->setContactTestBitmask(WALL_CONTACTTESTBITMASK); setInvincibleAtLine(); //检查过,切换场景会被删 SelfPro ua; ua.isInvincible = 1; ContactPro rs; setSelfPro(ua); setContactPro(rs); }
void GameScene::addHeart(TMXTiledMap *tmx){ ValueVector objects = map->getObjectGroup("heart")->getObjects(); for (int i = 0; i < objects.size(); i++) { ValueMap& dict = objects.at(i).asValueMap(); float x = dict["x"].asFloat(); float y = dict["y"].asFloat(); Sprite *heart = Sprite::create("heart.png"); auto physicsBody = PhysicsBody::createCircle(heart->getContentSize().width/2, PhysicsMaterial(0, 0, 1)); physicsBody->setGravityEnable(false); heart->setPhysicsBody(physicsBody); //加了物理属性后heart不跟随tmx一起移动 heart->setPosition(Point(x, y)); tmx->addChild(heart); } }
void BreakoutMainScene::createPaddle() { // Create Paddle paddle = Sprite::create("breakout_img/Paddle.png"); auto paddleBody = PhysicsBody::createEdgeBox(Size(paddle->getContentSize().width, paddle->getContentSize().height) , PHYSICSBODY_MATERIAL_DEFAULT); paddleBody->getShape(0)->setDensity(1.0f); // Ty trong,.mat do paddleBody->getShape(0)->setFriction(0.0f); // Ma sat paddleBody->getShape(0)->setRestitution(1.0f); // Dan hoi paddleBody->setGravityEnable(false); paddleBody->setDynamic(false); // Tinh khi tuong tac paddleBody->setContactTestBitmask(0x00000001); paddle->setPhysicsBody(paddleBody); paddle->setAnchorPoint(Vec2::ANCHOR_MIDDLE); paddle->setPosition(Vec2(WINSIZE.width/2 , paddle->getContentSize().height / 2 + 10)); paddle->setTag(Tag::T_Paddle); this->addChild(paddle); }
Bullet* Bullet::create(int bulletFileIndex) { Bullet* bullet = new Bullet(); if (bullet->initWithFile(ResourceLoader::getBulletFile(bulletFileIndex))) { bullet->autorelease(); bullet->scheduleUpdate(); bullet->setScale(Director::getInstance()->getWinSize().width * 0.8 / 800); bullet->hit = false; auto spriteBody = PhysicsBody::createBox(bullet->boundingBox().size, PhysicsMaterial(1.0f, 0.5f, 0.5f)); spriteBody->setGravityEnable(false); spriteBody->setCollisionBitmask(BULLET_COLLISION_BITMASK); spriteBody->setContactTestBitmask(true); bullet->setPhysicsBody(spriteBody); return bullet; } CC_SAFE_DELETE(bullet); return NULL; }
void PhysicsBody::setDynamic(bool dynamic) { if (dynamic != _dynamic) { _dynamic = dynamic; if (dynamic) { cpBodySetMass(_info->getBody(), _mass); cpBodySetMoment(_info->getBody(), _moment); if (_world != nullptr) { // reset the gravity enable if (isGravityEnabled()) { _gravityEnabled = false; setGravityEnable(true); } cpSpaceAddBody(_world->_info->getSpace(), _info->getBody()); } } else { if (_world != nullptr) { cpSpaceRemoveBody(_world->_info->getSpace(), _info->getBody()); } // avoid incorrect collion simulation. cpBodySetMass(_info->getBody(), PHYSICS_INFINITY); cpBodySetMoment(_info->getBody(), PHYSICS_INFINITY); cpBodySetVel(_info->getBody(), cpvzero); cpBodySetAngVel(_info->getBody(), 0.0f); resetForces(); } } }
void Game::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event){ printf("ed"); //下面是主角跟随路线移动 ballOne=Sprite::create("01.jpg"); auto mainBox=PhysicsBody::createBox(ballOne->getContentSize()); mainBox->setGravityEnable(false); mainBox->setContactTestBitmask(0xFFFFFFFF); ballOne->setPhysicsBody(mainBox); //s->autorelease(); ballOne->setPosition(points[0]); addChild(ballOne); //下面是填装动作的容器 Vector<FiniteTimeAction*> actionVector; for (int i=0;i<10000;i++){ if (i!=0&&points[i].x!=0) { actionVector.pushBack(MoveTo::create(ccpSub(points[i-1], points[i]).length()/100, points[i])); } }if(isMoved){ auto allAction=Sequence::create(actionVector); ballOne->runAction(allAction); } isMoved=false; }
void BreakoutMainScene::createBlock() { for (int i = 0; i < BLOCK_NUMBER; i++) { auto block = Sprite::create("breakout_img/blocks.png"); auto blockBody = PhysicsBody::createBox(Size(block->getContentSize().width , block->getContentSize().height) , PHYSICSBODY_MATERIAL_DEFAULT); blockBody->getShape(0)->setRestitution(0.0f); // dan hoi blockBody->getShape(0)->setFriction(0.0f); // ma sat blockBody->getShape(0)->setDensity(1.0f); // mat do blockBody->setContactTestBitmask(0x00000001); blockBody->setDynamic(false); blockBody->setGravityEnable(false); block->setPhysicsBody(blockBody); float offsetPosX = block->getContentSize().width/2 + 200*(i) + block->getContentSize().width; block->setPosition(Vec2(offsetPosX , 450)); block->setTag(Tag::T_Block); this->addChild(block); } }
bool Game1::onContactBegin(const PhysicsContact& contact) { Sprite* spriteA = (Sprite*)contact.getShapeA()->getBody()->getNode(); Sprite* spriteB = (Sprite*)contact.getShapeB()->getBody()->getNode(); int tagA = spriteA->getTag(); int tagB = spriteB->getTag(); if (tagA >= 3) { spriteA->removeFromParentAndCleanup(true); counts--; if (tagA == 3) { paddle->setScale(1.35f); } if (tagA == 4) { paddle->setScale(0.8f); } if (tagA == 5) { paddle->setScale(1.2f); } if (tagA == 10) { flag = 1; ba2 = Ball::create(); ba2->bindSprite(Sprite::create("Game/Level_1/ball.png")); this->addChild(ba2); ball2 = ba2->getSprite(); ball2->setPosition(100, 100); auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width / 2.); ballBody->getShape(0)->setRestitution(1.0f); ballBody->getShape(0)->setFriction(0.0f); ballBody->getShape(0)->setDensity(1.0f); ballBody->setGravityEnable(false); Vect force = Vect(40000.0f, 40000.0f); ballBody->applyImpulse(force); ballBody->setCategoryBitmask(0x0001); ballBody->setCollisionBitmask(0x0001); ballBody->setContactTestBitmask(0x0001); ball2->setPhysicsBody(ballBody); ball2->setTag(1); ball2->setScale(1.1f); } } if (tagB >= 3) { spriteB->removeFromParentAndCleanup(true); counts--; if (tagB == 3) { paddle->setScale(1.35f); } if (tagB == 4) { paddle->setScale(0.8f); } if (tagB == 5) { paddle->setScale(1.2f); } if (tagB == 10) { flag = 1; ba2 = Ball::create(); ba2->bindSprite(Sprite::create("Game/Level_1/ball.png")); this->addChild(ba2); ball2 = ba2->getSprite(); ball2->setPosition(100, 100); auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width / 2.); ballBody->getShape(0)->setRestitution(1.0f); ballBody->getShape(0)->setFriction(0.0f); ballBody->getShape(0)->setDensity(1.0f); ballBody->setGravityEnable(false); Vect force = Vect(40000.0f, 40000.0f); ballBody->applyImpulse(force); ballBody->setCategoryBitmask(0x0001); ballBody->setCollisionBitmask(0x0001); ballBody->setContactTestBitmask(0x0001); ball2->setPhysicsBody(ballBody); ball2->setTag(1); ball2->setScale(1.1f); } } return true; }
bool Game1::init() { if (!Layer::init()) { return false; } padding = 70; counts = 25; flag = 0; //The Size seting. auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); //Keyboard seting. this->setKeyboardEnabled(true); this->setKeypadEnabled(true); //create the background auto BackgroundSprite = Sprite::create("Game/Level_1/background.png"); BackgroundSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); addChild(BackgroundSprite); //create the bound. edgeSp = Sprite::create(); auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(1.0f, 1.0f, 0.0f), 3); edgeSp->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2)); edgeSp->setTag(0); edgeSp->setPhysicsBody(boundBody); this->addChild(edgeSp); //create the ball ba = Ball::create(); ba->bindSprite(Sprite::create("Game/Level_1/ball.png")); this->addChild(ba); ball = ba->getSprite(); ball->setPosition(100, 100); auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width / 2, PhysicsMaterial(1.0f, 1.0f, 0.0f)); ballBody->setGravityEnable(false); Vect force = Vect(40000.0f, 40000.0f); ballBody->applyImpulse(force); ballBody->setCategoryBitmask(0x0001); ballBody->setCollisionBitmask(0x0001); ballBody->setContactTestBitmask(0x0001); ball->setPhysicsBody(ballBody); ball->setTag(1); //create the paddle. pa = Paddle::create(); pa->bindSprite(Sprite::create("Game/Level_1/paddle.png")); paddle = pa->getSprite(); this->addChild(pa); auto paddleBody = PhysicsBody::createBox(paddle->getContentSize(), PhysicsMaterial(10.0f, 1.0f, 0.0f)); paddleBody->setGravityEnable(false); paddleBody->setDynamic(false); paddle->setPhysicsBody(paddleBody); paddle->setTag(2); paddle->setPosition(visibleSize.width / 2, visibleSize.height * 0.05); //add the pink block. Sprite* block; bl = Block::create(); bl->bindSprite(Sprite::create("block/block4.png")); this->addChild(bl); block = bl->getSprite(); auto blockBody = PhysicsBody::createBox(block->getContentSize(), PhysicsMaterial(10.0f, 1.0f, 0.0f)); blockBody->setDynamic(false); blockBody->setCategoryBitmask(0x0001); blockBody->setCollisionBitmask(0x0001); blockBody->setContactTestBitmask(0x0001); block->setPhysicsBody(blockBody); block->setTag(10); for (int j = 0; j < 5; j++) { int Xoffset = 120; for (int i = 0; i < 5; i++) { Xoffset = padding + Xoffset; if (j == 4 && i == 4) { block->setPosition(Vec2(Xoffset, visibleSize.height*0.8 - j * 70)); } else { addBlock(Vec2(Xoffset, visibleSize.height*0.8 - j * 70)); } } } this->schedule(schedule_selector(Game1::callback4)); //add the pause btn. auto item1 = MenuItemImage::create("Game/btn_pause.png", "Game/btn_pause.png",CC_CALLBACK_1(Game1::call, this)); auto menu = Menu::create(item1, NULL); menu->setPosition(Vec2(visibleSize.width * 0.9, visibleSize.height * 0.95)); addChild(menu, 1); //eventlistener auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(Game1::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }
bool Player::init() { if(!Node::init()) return false; //定义男孩和女孩的序列帧动画,注意此处要经常切换纹理,所以不能用AnimationCache Animation *girlAnimation=Animation::create(); for(int i=1;i<=8;i++) girlAnimation->addSpriteFrameWithFile("girl"+std::to_string(i)+".png"); girlAnimation->setDelayPerUnit(0.15f); Animation *boyAnimation=Animation::create(); for(int i=1;i<=12;i++) boyAnimation->addSpriteFrameWithFile("boy"+std::to_string(i)+".png"); boyAnimation->setDelayPerUnit(0.1f); //选取不同角色 std::string playerTextureName; std::string playerJumpTexureName; std::string playerSlideTextureName; Animation *playerAnimation; //添加游戏角色,分男女 switch(playerType) { case BOY: playerTextureName="boy1.png"; playerJumpTexureName="boy_jump.png"; playerSlideTextureName="boy_slide.png"; playerAnimation=boyAnimation; break; case GIRL: playerTextureName="girl1.png"; playerJumpTexureName="girl_jump.png"; playerSlideTextureName="girl_slide.png"; playerAnimation=girlAnimation; break; } playerSprite=Sprite::create(playerTextureName); //此处必须初始化一张角色纹理,否则后面无法切换纹理 jumpTexture=Sprite::create(playerJumpTexureName)->getTexture(); //创建跳跃纹理 slideTexture=Sprite::create(playerSlideTextureName)->getTexture(); //创建滑行纹理 playerAnim=Animate::create(playerAnimation); this->addChild(playerSprite); auto playerBody=PhysicsBody::createBox(playerSprite->getContentSize()); //这里要用包围盒,如果用圆形的话会导致滚动 playerBody->setDynamic(true); playerBody->setContactTestBitmask(1); playerBody->setGravityEnable(true); playerBody->getShape(0)->setRestitution(0.0f); //设置刚体回弹力 this->setPhysicsBody(playerBody); playerSprite->runAction(RepeatForever::create(playerAnim)); //初始时刻角色在奔跑 playerState=RUN; return true; }
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; }
//キキちゃんの初期設定 Kiki::Kiki(){ //キャラクタースプライトの生成(UP時) kiki = Sprite::create("kiki_up.png"); //影 kikiShadow = Sprite::create("kiki_up_shadow.png"); //キャラクタースプライトの大きさ kiki -> setScale(0.5); //影を実体に入れちゃう kiki->addChild(kikiShadow); //影のポジション調整 kikiShadow->setPosition(kiki->getContentSize().width/2,kiki->getContentSize().height/2); //影の透明度を設定 kikiShadow->setOpacity(180); kikiShadow->setGlobalZOrder(zOrderOfKikiShadow); //影の大きさ(実体のスケールをgetしてます)※特にスケールせずにぴったり入りました(謎) //kikiShadow -> setScale(kiki->getScale()); //ポジションの設定 kiki->setPosition(Vec2(selfFrame.width/4, selfFrame.height/2)); //zポジションの設定 kiki->setGlobalZOrder(zOrderOfKiki); //タグつけ kiki->setName("kiki"); //タグで上昇か下降か判断してみる(1は上昇、0は下降) kiki->setTag(1); //物理体の生成 auto kikiMaterial = PHYSICSBODY_MATERIAL_DEFAULT; //auto kikiBody = PhysicsBody::createCircle((kiki->getContentSize().width/2),kikiMaterial); Point spritePoints[5]={ //Vec2(-10,-22),Vec2(-18,0),Vec2(-10,20),Vec2(15,15),Vec2(10,-20) Vec2(-8,-20),Vec2(-16,0),Vec2(-8,18),Vec2(13,13),Vec2(8,-18) }; auto kikiBody = PhysicsBody::createPolygon(spritePoints, 5,kikiMaterial); //重力による影響の可否 kikiBody->setGravityEnable(false); //まじない kikiBody->setDynamic(true); kikiBody->setEnable(false); //ビットマスクはてきとう kikiBody->setCategoryBitmask(0x01); kikiBody->setCollisionBitmask(0); kikiBody->setContactTestBitmask(0x02); kiki->setPhysicsBody(kikiBody); //箒の設定 broom = Sprite::create(); broom -> setTextureRect(Rect(0, 0, 10, 10)); broom -> setPosition(Vec2(0,0)); broom -> setVisible(false); auto broomMaterial = PHYSICSBODY_MATERIAL_DEFAULT; Point broomPoint[3]={ //Vec2(0,0), Vec2(0,40), Vec2(180,35) Vec2(10,5), Vec2(10,35), Vec2(160,35), }; auto broomBody = PhysicsBody::createPolygon(broomPoint,3,broomMaterial); broomBody -> setGravityEnable(false); broomBody -> setDynamic(true); broomBody -> setEnable(false); broomBody ->setCategoryBitmask(0x01); broomBody ->setCollisionBitmask(0); broomBody ->setContactTestBitmask(0x02); broom -> setPhysicsBody(broomBody); kiki -> addChild(broom); endParticle = ParticleSystemQuad::create("particleFlower.plist"); //retainしないと勝手に解放されて後々エラーへ endParticle->retain(); kikiParticle = ParticleSystemQuad::create("kikiparticle.plist"); kikiParticle->setAnchorPoint(Vec2(0.5f,0.5f)); kikiParticle->setPosition(Vec2(3,kiki->getContentSize().height/3-6)); kikiParticle->setName("kikiParticle"); kikiParticle->setGlobalZOrder(zOrderOfKikiShadow); kiki->addChild(kikiParticle); }
//ハリーポッターの初期設定 HarryPotter::HarryPotter(){ //キャラクタースプライトの生成(UP時) harryPotter = Sprite::create("harryPotter.png"); //影 harryPotterShadow = Sprite::create("harryPotter_shadow.png"); //キャラクタースプライトの大きさ harryPotter -> setScale(0.5); //影を実体に入れちゃう harryPotter->addChild(harryPotterShadow); //影のポジション調整 harryPotterShadow->setPosition(harryPotter->getContentSize().width/2,harryPotter->getContentSize().height/2); //影の透明度を設定 harryPotterShadow->setOpacity(180); harryPotterShadow->setGlobalZOrder(zOrderOfKikiShadow); //影の大きさ(実体のスケールをgetしてます)※特にスケールせずにぴったり入りました(謎) //harryPotterShadow -> setScale(harryPotter->getScale()); //ポジションの設定 harryPotter->setPosition(Vec2(selfFrame.width/4, selfFrame.height/2)); //zポジションの設定 harryPotter->setGlobalZOrder(zOrderOfKiki); //タグつけ harryPotter->setName("kiki"); //タグで上昇か下降か判断してみる(1は上昇、0は下降) harryPotter->setTag(1); //物理体の生成 auto harryPotterMaterial = PHYSICSBODY_MATERIAL_DEFAULT; //auto harryPotterBody = PhysicsBody::createCircle((harryPotter->getContentSize().width/2),harryPotterMaterial); Point spritePoints[8]={ Vec2(-8,-20),Vec2(-40,-5),Vec2(-20,20), Vec2(-5,22),Vec2(13,13),Vec2(15,-5) }; auto harryPotterBody = PhysicsBody::createPolygon(spritePoints, 8,harryPotterMaterial); //重力による影響の可否 harryPotterBody->setGravityEnable(false); //まじない harryPotterBody->setDynamic(true); harryPotterBody->setEnable(false); //ビットマスクはてきとう harryPotterBody->setCategoryBitmask(0x01); harryPotterBody->setCollisionBitmask(0); harryPotterBody->setContactTestBitmask(0x02); harryPotter->setPhysicsBody(harryPotterBody); //箒の設定 broom = Sprite::create(); broom -> setTextureRect(Rect(0, 0, 10, 10)); broom -> setPosition(Vec2(0,0)); broom -> setVisible(false); auto broomMaterial = PHYSICSBODY_MATERIAL_DEFAULT; Point broomPoint[4]={ Vec2(23,5), Vec2(0,15), Vec2(30,25), Vec2(150,32) }; auto broomBody = PhysicsBody::createPolygon(broomPoint,4,broomMaterial); broomBody -> setGravityEnable(false); broomBody -> setDynamic(true); broomBody -> setEnable(false); broomBody ->setCategoryBitmask(0x01); broomBody ->setCollisionBitmask(0); broomBody ->setContactTestBitmask(0x02); broom -> setPhysicsBody(broomBody); harryPotter -> addChild(broom); endParticle = ParticleSystemQuad::create("particleFlower.plist"); //retainしないと勝手に解放されて後々エラーへ endParticle->retain(); harryPotterParticle = ParticleSystemQuad::create("kikiparticle.plist"); harryPotterParticle->setAnchorPoint(Vec2(0.5f,0.5f)); harryPotterParticle->setPosition(Vec2(3,harryPotter->getContentSize().height/3-6)); harryPotterParticle->setName("kikiParticle"); harryPotterParticle->setGlobalZOrder(zOrderOfKikiShadow); harryPotter->addChild(harryPotterParticle); }
void Game::setUI(){ //测试在界面上放置组件 Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // auto gameLabel=Label::createWithTTF("GAME", "fonts/Marker Felt.ttf", 24); // gameLabel->setPosition(Vec2(origin.x + visibleSize.width/2, // origin.y + visibleSize.height - gameLabel->getContentSize().height)); // this->addChild(gameLabel); auto image1=MenuItemImage::create("01.jpg", "01.jpg"); image1->setPosition(100, 100); auto menu=Menu::create(image1,NULL); menu->setPosition(100,100); // this->addChild(menu,1); //下面 测试节点的运动 // auto moveTo=MoveTo::create(3.0f, Vec2(origin.x + visibleSize.width/2, // origin.y + visibleSize.height - gameLabel->getContentSize().height-200)); //auto action=Sequence::create(moveTo,Spawn::create(RotateBy::create(1.0f,360),ScaleTo::create(1.0f,1.2f),NULL),Blink::create(1,5),FadeOut::create(0.5f), NULL); //image1->runAction(action); //下面测试动画的实现 //下面测试触控 // sprite=CCSprite::create("03.jpg"); // sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // sprite->setScale(1, 0.5); auto sprite1=Sprite::create("00.jpg"); sprite1->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); auto wallBody1=PhysicsBody::createBox(sprite1->getContentSize()); CCLOG("%f,%f",sprite1->getContentSize().width,sprite1->getContentSize().height); wallBody1->setGravityEnable(false); // wallBody1->getShape(0)->setRestitution(1.0f); // wallBody1->getShape(0)->setFriction(0.0); // wallBody1->getShape(0)->setDensity(1.0); // Vect force = Vect(500000.0f, 500000.0f); // wallBody1->applyImpulse(force); wallBody1->setContactTestBitmask(0xFFFFFFFF); sprite1->setPhysicsBody(wallBody1); sprite1->setTag(1); this->addChild(sprite1); auto sprite2=Sprite::create("01.jpg"); sprite2->setPosition(Vec2(300 , 250)); auto wallBody2=PhysicsBody::createBox(sprite2->getContentSize()); wallBody2->setGravityEnable(false); wallBody2->setContactTestBitmask(0xFFFFFFFF); sprite2->setPhysicsBody(wallBody2); sprite2->setTag(2); this->addChild(sprite2); sprite2->runAction(MoveTo::create(2, Vec2(400, 400))); Sprite* edgeSpace=Sprite::create(); PhysicsBody* 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(Point(visibleSize.width/2,visibleSize.height/2)); this->addChild(edgeSpace); edgeSpace->setTag(0); auto listener=EventListenerTouchOneByOne::create(); listener->onTouchMoved=CC_CALLBACK_2(Game::onTouchMoved, this); listener->onTouchBegan=CC_CALLBACK_2(Game::onTouchBegan, this); listener->onTouchEnded=CC_CALLBACK_2(Game::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //下面检测碰撞 }
bool LevelTwentytwo::init() { if (!Layer::init()) { return false; } // 用于解决刚体穿透问题 this->scheduleUpdate(); // 计时器 this->schedule(schedule_selector(LevelTwentytwo::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); ballOne = Sprite::createWithSpriteFrameName("hero.png"); ballOne->setPosition(visibleSize.width / 2.0f, ballOne->getContentSize().height); 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(8);// 接触测试掩码 // 把物体添加到精灵 ballOne->setPhysicsBody(ballBodyOne); this->addChild(ballOne); auto girl = Sprite::createWithSpriteFrameName("girl.png"); girl->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 250.0f); auto girlBody = PhysicsBody::createCircle(girl->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); girlBody->setDynamic(false); girlBody->setGravityEnable(false); girlBody->setCategoryBitmask(8); girlBody->setCollisionBitmask(1); girlBody->setContactTestBitmask(1); girl->setPhysicsBody(girlBody); this->addChild(girl); //创建一个盒子,用来碰撞 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); boundBody->setContactTestBitmask(0); // 随机位置 int index = CCRANDOM_0_1() * 4; auto suiji1 = 0; auto suiji2 = 0; auto suiji3 = 0; switch (index) { case 0: suiji1 = CCRANDOM_0_1() * 240; suiji2 = -CCRANDOM_0_1() * 100; suiji3 = -CCRANDOM_0_1() * 150; break; case 1: suiji1 = -CCRANDOM_0_1() * 240; suiji2 = -CCRANDOM_0_1() * 100; suiji3 = -CCRANDOM_0_1() * 150; break; case 2: suiji1 = -CCRANDOM_0_1() * 240; suiji2 = CCRANDOM_0_1() * 100; suiji3 = CCRANDOM_0_1() * 150; break; case 3: suiji1 = CCRANDOM_0_1() * 240; suiji2 = CCRANDOM_0_1() * 100; suiji3 = CCRANDOM_0_1() * 150; break; default: break; } // 创建yuan auto white1 = Sprite::createWithSpriteFrameName("white2.png"); white1->setPosition(visibleSize.width / 2.0f + 200.0f, visibleSize.height / 2.0f - 150.0f); auto whiteBody1 = PhysicsBody::createCircle(white1->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); whiteBody1->setDynamic(false); whiteBody1->setCategoryBitmask(4); whiteBody1->setCollisionBitmask(1 | 2 | 4); whiteBody1->setContactTestBitmask(0); white1->setPhysicsBody(whiteBody1); this->addChild(white1); // 正方形 auto white1_2 = Sprite::createWithSpriteFrameName("white1.png"); white1_2->setPosition(visibleSize.width / 2.0f - 200.0f, visibleSize.height / 2.0f + 150.0f); 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); 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"); white3->setPosition(visibleSize.width / 2.0f + suiji3, visibleSize.height / 2.0f + suiji2); auto whiteBody3 = PhysicsBody::createPolygon(arr, 3, PHYSICSBODY_MATERIAL_DEFAULT); whiteBody3->setDynamic(false); whiteBody3->setCategoryBitmask(4); whiteBody3->setCollisionBitmask(1 | 2 | 4); whiteBody3->setContactTestBitmask(0); white3->setPhysicsBody(whiteBody3); this->addChild(white3); 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 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); yuantongBody->setContactTestBitmask(0); yuantong0->setPhysicsBody(yuantongBody); this->addChild(yuantong0); auto yuantong1 = Sprite::createWithSpriteFrameName("yuantong.png"); yuantong1->setPosition(Vec2(visibleSize.width / 2.0f - yuantong1->getContentSize().width / 2 * 3, visibleSize.height + yuantong1->getContentSize().height / 2.0f)); auto yuantongBody1 = PhysicsBody::createBox(yuantong1->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); yuantongBody1->setDynamic(false); yuantongBody1->setCategoryBitmask(4); yuantongBody1->setCollisionBitmask(1 | 2 | 4); yuantongBody1->setContactTestBitmask(0); yuantong1->setPhysicsBody(yuantongBody1); this->addChild(yuantong1); auto moveby0 = MoveBy::create(0.5f, Vec2(0, -90)); auto moveby0back = moveby0->reverse(); auto easein = EaseBackIn::create(moveby0); auto seq0 = Sequence::create(easein, CallFuncN::create(CC_CALLBACK_1(LevelTwentytwo::callbackC, this)), moveby0back, nullptr); auto repeat0 = RepeatForever::create(seq0); yuantong0->runAction(repeat0); auto moveby1 = MoveBy::create(1.0f, Vec2(0, -90)); auto moveby1back = moveby1->reverse(); auto easein1 = EaseBackIn::create(moveby1); auto yuantongseq1 = Sequence::create(easein1, CallFuncN::create(CC_CALLBACK_1(LevelTwentytwo::callbackC, this)), moveby1back, nullptr); auto yuantongrepeat1 = RepeatForever::create(yuantongseq1); yuantong1->runAction(yuantongrepeat1); listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [=](Touch* touch, Event* event){ Vec2 force = Vec2::ZERO; force = Vec2(0, 50000.0f); ballOne->getPhysicsBody()->applyImpulse(force); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // 时间显示文本 test = LabelAtlas::create(StringUtils::toString(nTimes), "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); 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; }