当前位置: 首页>>代码示例>>C++>>正文


C++ CCSpriteBatchNode::convertToNodeSpace方法代码示例

本文整理汇总了C++中CCSpriteBatchNode::convertToNodeSpace方法的典型用法代码示例。如果您正苦于以下问题:C++ CCSpriteBatchNode::convertToNodeSpace方法的具体用法?C++ CCSpriteBatchNode::convertToNodeSpace怎么用?C++ CCSpriteBatchNode::convertToNodeSpace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CCSpriteBatchNode的用法示例。


在下文中一共展示了CCSpriteBatchNode::convertToNodeSpace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: update

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());
}
开发者ID:cqnature,项目名称:testrungame,代码行数:99,代码来源:GameLayer.cpp


注:本文中的CCSpriteBatchNode::convertToNodeSpace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。