void GameLayer::update(float dt) { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *playerSprite = (CCSprite *)this->getChildByTag(PlayerTag); CCPoint currentPos = playerSprite->getPosition(); // update run distance runDistance += (currentPos.y - prevPos.y); // stay the player at specified position if (currentPos.y > kPlayerStayAtScreenPosY) { CCPoint newPos = ccp(currentPos.x, kPlayerStayAtScreenPosY); CCPoint diff = ccpSub(newPos, currentPos); // move the player and map playerSprite->setPosition(newPos); // move map nodes for (int i = 0; i < kGameMapCount; i ++) { CCNode *mapNode = this->getChildByTag(MapStartTag + i); CCPoint mapPos = ccpAdd(mapNode->getPosition(), diff); mapNode->setPosition(mapPos); // if map get out of scene if (mapPos.y <= -winSize.height) { mapPos = ccp(mapPos.x, (kGameMapCount - 1) * winSize.height); mapNode->setPosition(mapPos); this->resetMapNode(mapNode); } } // move map barrier node for (int i = 0; i < kGameMapCount; i ++) { CCNode *barrierNode = this->getChildByTag(MapBarrierStartTag + i); CCPoint barrierPos = ccpAdd(barrierNode->getPosition(), diff); barrierNode->setPosition(barrierPos); // if map get out of scene if (barrierPos.y <= -winSize.height) { barrierPos = ccp(barrierPos.x, (kGameMapCount - 1) * winSize.height); barrierNode->setPosition(barrierPos); this->resetBarrierNode(barrierNode, MapBarrierStartTag + i); } } } // save current pos prevPos = playerSprite->getPosition(); // collision detection for (int i = 0; i < kGameMapCount; i ++) { CCNode *mapNode = this->getChildByTag(MapStartTag + i); CCSpriteBatchNode *batchNode = (CCSpriteBatchNode *)mapNode->getChildByTag(MapBatchNodeTag); CCPoint playerPos = batchNode->convertToNodeSpace(this->convertToWorldSpace(playerSprite->getPosition())); CCRect playerBox = CCRect(-playerSprite->getContentSize().width/2.0f + playerPos.x, -playerSprite->getContentSize().height/2.0f + playerPos.y, playerSprite->getContentSize().width, playerSprite->getContentSize().height); CCSprite *monsterSprite = (CCSprite *)batchNode->getChildByTag(MapMonsterTag); if (playerBox.intersectsRect(monsterSprite->boundingBox())) { // game over this->gameOver(); } CCSprite *riverSprite = (CCSprite *)batchNode->getChildByTag(MapRiverTag); if (playerBox.intersectsRect(riverSprite->boundingBox())) { // not in jumpping if (!isJumpping) { // game over this->gameOver(); } } for (int i = 0; i < kGameCoinCount; i ++) { CCSprite *coinSprite = (CCSprite *)batchNode->getChildByTag(MapCoinStartTag + i); if (playerBox.intersectsRect(coinSprite->boundingBox())) { coinSprite->setVisible(false); coinCount ++; } } } for (int i = 0; i < kGameMapCount; i ++) { CCNode *barrierNode = this->getChildByTag(MapBarrierStartTag + i); CCSpriteBatchNode *batchNode = (CCSpriteBatchNode *)barrierNode->getChildByTag(MapBatchNodeTag); CCPoint playerPos = batchNode->convertToNodeSpace(this->convertToWorldSpace(playerSprite->getPosition())); CCRect playerBox = CCRect(-playerSprite->getContentSize().width/2.0f + playerPos.x, -playerSprite->getContentSize().height/2.0f + playerPos.y, playerSprite->getContentSize().width, playerSprite->getContentSize().height); CCSprite *barrierSprite = (CCSprite *)batchNode->getChildByTag(MapBarrierTag); if (playerBox.intersectsRect(barrierSprite->boundingBox())) { if (!isSliding) { // game over this->gameOver(); } } } // update score label CCLabelTTF *scoreLabel = (CCLabelTTF *)this->getChildByTag(ScoreLabelTag); int totalScore = runDistance + coinCount; scoreLabel->setString(CCString::createWithFormat("score: %d", totalScore)->getCString()); }