0
私がAndroid開発を開始して以来、ちょっと待っています。この時点で私はゲームをセットアップしていて、すべてうまく動作し、高得点が更新され、最新のハイスコアはGameOverScene
にも表示されています。しかし、私はリストを作ってみたかったが、上位5つの高得点を挙げてみよう。UserDefault
に複数の得点を格納することはできないようだ。cocos2d-x:userdefaultでリーダーボードスコアを保存する
ここではGameOverScene
が呼び出され、currentScore
が渡されます。
GameScene.cpp
auto scene = GameOverScene::createScene(currentScore);
は、その後、私はこの
GameOverScene.cpp
にclass GameOverScene : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene(unsigned int tempScore);
currentScore = tempScore;
// rest of the code
}
bool GameOverScene::init()
{
..//
UserDefault *defScore = UserDefault::getInstance();
// auto highScore[] = defScore->getIntegerForKey("HIGH SCORE", 0);
auto highScore = defScore->getIntegerForKey("HIGH SCORE", 0);
// for(int i=0; i<=5; i++){
if(currentScore > highScore)
{
highScore = currentScore;
log("high score inside IF: %i", highScore);
defScore->setIntegerForKey("HIGH SCORE", highScore);
}
// }
defScore->flush();
__String *tempScore = __String::createWithFormat("%i", currentScore);
auto cScoreLabel = LabelTTF::create(tempScore->getCString(), "fonts/Marker Felt.ttf", visibleSize.height * SCORE_FONT_SIZE);
cScoreLabel->setPosition(Point(visibleSize.width * 0.25 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(cScoreLabel);
// for(int i=0; i < =5; i++){
__String *tempHighScore = __String::createWithFormat("%i", highScore);
auto highScoreLabel = LabelTTF::create(tempHighScore->getCString(), "fonts/Marker Felt.ttf", visibleSize.height * SCORE_FONT_SIZE);
highScoreLabel->setColor(Color3B::YELLOW);
highScoreLabel->setPosition(Point(visibleSize.width * 0.75 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(highScoreLabel);
// }
return true;
}