如何刷新精灵(删除并重新创建它)

时间:2014-07-14 10:28:39

标签: c++ cocos2d-x-2.x

我正在尝试在游戏中制作日夜背景模式,并且我想在选项对话框中创建一个控制按钮,当我点击它时,所有背景都会更改而不退出对话框。我刚刚通过重新打开场景让它运行正常,但它也退出了选项对话框。

我有这样的initBackground()方法

void MenuScene::initMenuBackground() {
setBackgroundMode();
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//calculate system hour time
time_t now = time(0);   // get time now
tm * ltm = localtime(&now);
double hour = ltm->tm_hour;
int curHourTime = (int) hour;
CCLOG("MenuScene hour is: %dh", curHourTime);
CCAnimation* caveAnim1 = CCAnimation::createWithSpriteFrames (senspark::Utils::createFramesArray("cave-%d.png", 3), 0.2f);
CCAnimation* caveAnim2 = CCAnimation::createWithSpriteFrames(senspark::Utils::createFramesArray("cave-%d.png", 3, 0, true), 0.2f);
cloudSpr->runAction(CCRepeatForever::create(CCSequence::create(CCMoveTo::create(30, ccp(winSize.width+100, cloudSpr->getPositionY())),
                                                               CCMoveTo::create(0, ccp(-100, cloudSpr->getPositionY())),
                                                               NULL)));
cloudNightSpr->runAction(CCRepeatForever::create(CCSequence::create(CCMoveTo::create(30, ccp(winSize.width+100, cloudSpr->getPositionY())),
                                                                    CCMoveTo::create(0, ccp(-100, cloudSpr->getPositionY())),
                                                                    NULL)));


//night
if ( ((curHourTime < 6 || curHourTime > 18) && (_isAuto==true)) || _isNight==true) {
    caveNightSpr->runAction(CCRepeatForever::create(CCSequence::create(CCAnimate::create(caveAnim1),
                                                                       CCAnimate::create(caveAnim2),
                                                                       CCDelayTime::create(0.2f),
                                                                       NULL)));
    cloudSpr->setVisible(false);
    startGoldSpr->setVisible(false);
    backgroundSpr->setVisible(false);
    backgroundSkySpr->setVisible(false);
    backgroundNightSpr->setScaleX(CCDirector::sharedDirector()->getWinSize().width/designSize.width);
    backgroundSkyNightSpr->setScaleX(CCDirector::sharedDirector()->getWinSize().width/designSize.width);
}
//daytime
if ( ((curHourTime > 6 && curHourTime < 18) && (_isAuto==true)) || _isDay==true) {
    caveSpr->runAction(CCRepeatForever::create(CCSequence::create(CCAnimate::create(caveAnim1),
                                                                  CCAnimate::create(caveAnim2),
                                                                  CCDelayTime::create(0.2f),
                                                                  NULL)));
    cloudNightSpr->setVisible(false);
    startGoldNightSpr->setVisible(false);
    backgroundNightSpr->setVisible(false);
    backgroundSkyNightSpr->setVisible(false);
    backgroundSpr->setScaleX(CCDirector::sharedDirector()->getWinSize().width/designSize.width);
    backgroundSkySpr->setScaleX(CCDirector::sharedDirector()->getWinSize().width/designSize.width);
}

我不知道如何刷新这些Sprite(删除然后重新调用它们)。 对不起,我的英语不好。任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:0)

根据我的理解,您需要从场景中删除精灵并再次添加。

  1. CCNode / Sprite有这个方法removeFromParentAndCleanup(bool cleanup)
  2. 您还可以从CCNode / Sprite中删除所有子项 removeAllChildrenWithCleanup(bool cleanup)。
  3. Node / Sprite可以分配特殊的setTag(),稍后可以使用removeChildWithTag()删除特殊的Sprite。人们通常会使用枚举来识别关键元素。
  4. 我希望能回答你的问题。

    Refrences: http://www.cocos2d-x.org/reference/native-cpp/V2.2.3/d9/d1f/group__base__nodes.html#ga5889948f4c40933e93aaf70cb8846192

答案 1 :(得分:0)

最后找出我的问题,我可以使用mySprite->setTexture(CCTextureCache::sharedTextureCache()->addImage("newImage.png")); 更改精灵图像非常简单 不再需要删除然后再添加它。