Search code examples
c++cocos2d-x-2.x

How to refresh sprites (remove and recreate it again)


I am trying to make a day-night background mode in my game and I want to create a control button in a option dialog that when I click on it, all background are change without exiting the dialog. I have just made it run OK by re-open the scene but it also quit the option dialog.

I have an initBackground() method like this

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);
}

and I don't know how to refresh these Sprite (remove and then recall them again). Sorry for my bad English. Any help would be appreciated.


Solution

  • Finally figure out my issue, it's very simple that I can change the sprite image by using mySprite->setTexture(CCTextureCache::sharedTextureCache()->addImage("newImage.png")); no more need to remove and then add it again.