2016-08-08 3 views
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; 
} 

答えて

0

をしていますGameOverScene.cppにあなたは、単に複数のキーを格納することができます。 High_Score1、High_Score2と同様.... 現在のスコアを取得します。 「すべてのキーと比較」、「現在のスコアが小さい」の適切なキーに保存します。

関連する問題