GameScene* GameScene::create(const int gameType) { GameScene* result = new GameScene(); if (result->init()) { GameLayer* layer = new GameLayer(gameType); layer->autorelease(); result->addChild(layer); return result; } delete result; return NULL; }
void InputLayer::update(float delta) { GameScene *game = GameScene::getSharedGameScene(); totalTime += delta; if (fireButton->getIsActive() && totalTime > nextShotTime) { nextShotTime = totalTime + 0.5f; DogEntity* trendoc = game->getDog(); Point trendocPos = CCPointMake(trendoc->getPosition().x + trendoc->getContentSize().width * 0.5f, trendoc->getPosition().y); float spread = (CCRANDOM_0_1() - 0.5f) * 0.5f; Point velocity = CCPointMake(15, 0); Bullet *bullet = Bullet::initWithBulletForDoc(); bullet->setTag(game->getDogBulletTag()); game->addDogBulletTag(); game->addChild(bullet); bullet->shootBulletAt(trendocPos, velocity, "bullet.png"); } if (!fireButton->getIsActive()) { nextShotTime = 0; } DogEntity *trendoc = game->getDog(); Point velocity = ccpMult(joystick->getVelocity(), 200); if (velocity.x != 0 && velocity.y != 0) { float newX = trendoc->getPosition().x + velocity.x * delta; float newY = trendoc->getPosition().y + velocity.y * delta; float halfWidth = trendoc->getContentSize().width * 0.5f; float halfHeight = trendoc->getContentSize().height * 0.5f; Size screensize = Director::getInstance()->getVisibleSize(); if (newX < halfWidth) { newX = halfWidth; }else if (newX > (screensize.width - halfWidth)) { newX = screensize.width - halfWidth; } if (newY < halfHeight) { newY = halfHeight; }else if (newY > (screensize.height - halfHeight)) { newY = screensize.height - halfHeight; } trendoc->setPosition(newX, newY); } }
Radar::Radar(cocos2d::Layer & pGS, Point position) { this->_pGs = &pGS; this->radarBg1 = Sprite::create("radarBG1.png"); this->radarBg1->setPosition(position); GameScene* gs = (GameScene*) _pGs; gs->addChild(radarBg1); //populate the vector with the size of the mapinfo for ( int i = 0; i < (gs->getMapInfo()->GetTilesPerLine() * gs->getMapInfo()->GetTilesPerColumn()); i++ ) { this->map.push_back(RadarMapNode::Nothing); } }
//处理碰撞土块 void TileSprite::handleContact(MarioSprite * pMario) { //无效土块判断 if (m_eTileType == TSType_Invalid) return; Vec2 pos = getPosition(); bool bMarioTumble = (pMario->getMarioStatus() == MarioSt_LTumble || pMario->getMarioStatus() == MarioSt_RTumble); //是否翻跟头 m_bFinishHandleContact = false; //播放碰撞音效 if (!bMarioTumble) { if (m_eTileType == TSType_Null && pMario->getMarioLevel() > 1) playEffect(Effect_DestroyTile); else playEffect(Effect_JackSolid); } if (m_eTileType == TSType_Null) //空土块 { if (pMario->getMarioLevel() > 1) //正常马里奥的碰撞 { GameScene * gameScene = GameScene::getInstance(); Sprite * broken1 = Sprite::createWithSpriteFrameName("tile/broken_tile1.png"); broken1->setPosition(Vec2(pos.x - 10, pos.y + 10)); broken1->setCameraMask(2); gameScene->addChild(broken1, 10); Sprite * broken2 = Sprite::createWithSpriteFrameName("tile/broken_tile2.png"); broken2->setPosition(pos); broken2->setCameraMask(2); gameScene->addChild(broken2, 10); Sprite * broken3 = Sprite::createWithSpriteFrameName("tile/broken_tile3.png"); broken3->setPosition(Vec2(pos.x + 10, pos.y - 20)); broken3->setCameraMask(2); gameScene->addChild(broken3, 10); broken1->runAction(Sequence::create(JumpTo::create(bMarioTumble ? 0.5f : 1.0f, Vec2(pos.x - 350, -50.0f), bMarioTumble ? 150 : 300.0f, 1), CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, broken1)), nullptr)); broken2->runAction(Sequence::create(JumpTo::create(bMarioTumble ? 0.5f : 1.0f, Vec2(pos.x - 50.0f, -50.0f), bMarioTumble ? 150 : 300.0f, 1), CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, broken2)), nullptr)); broken3->runAction(Sequence::create(JumpTo::create(bMarioTumble ? 0.5f : 1.0f, Vec2(pos.x + 390.0f, -50.0f), bMarioTumble ? 150 : 300.0f, 1), CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, broken3)), nullptr)); this->removeFromParent(); } else //缩小版马里奥的碰撞 { //设置基础动作 ActionInterval * baseAction = Spawn::create( MoveBy::create(0.1f, Vec2(0.0f, bMarioTumble ? -20.0f : 20.0f)), CCScaleBy::create(0.1f, 1.3f), nullptr); this->runAction(Sequence::create( baseAction, baseAction->reverse(), CallFunc::create(CC_CALLBACK_0(TileSprite::finishHandleContact, this)), nullptr)); } } else if (m_eTileType == TSType_Coin) //金币土块 { if (m_nCoinCount <= 0) { setInvalid(); return; } //设置基础动作 ActionInterval * baseAction = Spawn::create(MoveBy::create(0.1f, Vec2(0.0f, bMarioTumble ? -20.0f : 20.0f)), CCScaleBy::create(0.1f, 1.3f), nullptr); //根据金币数目设置土块动作 if (m_nCoinCount == 1) { this->runAction(Sequence::create( baseAction, baseAction->reverse(), CallFunc::create(CC_CALLBACK_0(TileSprite::setInvalid, this)), CallFunc::create(CC_CALLBACK_0(TileSprite::finishHandleContact, this)), nullptr)); } else if (m_nCoinCount > 1) { this->runAction(Sequence::create( baseAction, baseAction->reverse(), CallFunc::create(CC_CALLBACK_0(TileSprite::finishHandleContact, this)), nullptr)); } --m_nCoinCount; //设置金币动作 Sprite * pCoin = Sprite::createWithSpriteFrameName("coin/coin1.png"); Size size = getContentSize(); pCoin->setPosition(Vec2(size.width / 2, size.height / 2)); pCoin->setCameraMask(2); this->addChild(pCoin, -1); Animation * pAnimation = AnimationCache::getInstance()->getAnimation("fastCoin"); pCoin->runAction(Animate::create(pAnimation)); ActionInterval * moveAction = nullptr; if (bMarioTumble) moveAction = MoveBy::create(0.3f, Vec2(0.0f, -100.0f)); else moveAction = JumpBy::create(0.7f, Vec2(0.0f, 50.0f), 100.0f, 1); pCoin->runAction(Sequence::create(moveAction, CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, pCoin)), CallFunc::create(CC_CALLBACK_0(TileSprite::addCoinCount, this)), nullptr)); playEffect(Effect_GetCoin); } else if (m_eTileType == TSType_Swamm) { //设置基础动作 ActionInterval * baseAction = Spawn::create(MoveBy::create(0.1f, Vec2(0.0f, bMarioTumble ? -20.0f : 20.0f)), CCScaleBy::create(0.1f, 1.3f), nullptr); this->runAction(Sequence::create(baseAction, baseAction->reverse(), CallFunc::create(CC_CALLBACK_0(TileSprite::setInvalid, this)), CallFunc::create(CC_CALLBACK_0(TileSprite::finishHandleContact, this)), CallFunc::create(CC_CALLBACK_0(TileSprite::addSwammSprite, this, bMarioTumble)), nullptr)); playEffect(Effect_SwammShow); } if (!bMarioTumble) { //执行AABB检验,判断是否在土块上面有怪兽、是否新一轮跟头碰撞 Size size = getContentSize(); float fPtm = getPTMRatio(); b2AABB aabb; aabb.lowerBound.Set((pos.x - size.width / 2) / fPtm, (pos.y - size.height / 2) / fPtm); aabb.upperBound.Set((pos.x + size.width / 2) / fPtm, (pos.y + (size.height) / 2 + 5) / fPtm); TileQueryCallBack tileCallBack(this); GameScene::getInstance()->getWorld()->QueryAABB(&tileCallBack, aabb); } }