2017-05-20 82 views
0

我遇到了cocos2dx中的字符串問題& C++。我想將可變背景傳遞給Sprite::create(background)但是,我得到一個錯誤。如果它是在Java中,下面的代碼將工作,但由於我不習慣C++,它可能會有所不同。另外,如果它是一個int我怎麼會通過它?我將如何解決這個問題?一些提示或樣品將會很棒!我很樂意聽取您的意見!無法將字符串變量傳遞給cocos2dx中的Sprite

void GameLayer::initBackground() 
{ 
    UserDefault *_userDef = UserDefault::getInstance(); 
    //int型 
    auto _int =_userDef->getIntegerForKey("back"); 
    auto string background = "Background1.png"; 
    if (_int == 0) { 
     background = "Background2.png"; 
    } 
    auto bgForCharacter = Sprite::create(background); 
    bgForCharacter->setAnchorPoint(Point(0, 1)); 
    bgForCharacter->setPosition(Point(0, WINSIZE.height)); 
    addChild(bgForCharacter, ZOrder::BgForCharacter); 

    auto bgForPuzzle = Sprite::create("Background2.png"); 
    bgForPuzzle->setAnchorPoint(Point::ZERO); 
    bgForPuzzle->setPosition(Point::ZERO); 
    addChild(bgForPuzzle, ZOrder::BgForPuzzle); 
} 

回答

2
auto userDefault=UserDefault::getInstance(); 

int value=userDefault->getIntegerForKey("back"); //find value for back if exist then it return that value else return 0 

auto sprite = Sprite::create(value==0?"Background2.png":"Background1.png"); 
sprite->setPosition(100, 100); 
this->addChild(sprite, 0); 

當你想改變你的背景只是把任何價值,而不是0

UserDefault::getInstance()->setIntegerForKey("back", 1); 
相關問題