cocos2dx中无法将字符串变量传递给Sprite
Unable to pass the string variable to Sprite in cocos2dx
我对 cocos2dx 和 C++ 中的字符串有疑问。我想将变量背景传递给 Sprite::create(background)
但是,我收到错误消息。如果它在 java 中,则以下代码将起作用,但由于我不习惯 C++,它可能会有所不同。另外,如果它是一个整数,我将如何传递它?我将如何解决这个问题?一些提示或示例会很棒!我很乐意听取您的意见!
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);
}
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);
我对 cocos2dx 和 C++ 中的字符串有疑问。我想将变量背景传递给 Sprite::create(background)
但是,我收到错误消息。如果它在 java 中,则以下代码将起作用,但由于我不习惯 C++,它可能会有所不同。另外,如果它是一个整数,我将如何传递它?我将如何解决这个问题?一些提示或示例会很棒!我很乐意听取您的意见!
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);
}
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);