CCUserDefault::sharedUserDefault()


#1

hi,

I have a bug at CCUserDefault::sharedUserDefault() (cocos2dx 2.1.4, iPad retina, iOS7). I am saving the network wins with it. But the counter resets very often after restarting the app. It do not resets to 0, it resets to the variable of the last start of the app.

The strange thing is, that the variable has the right value before closing the app. Seems to happen to the last few values written.

PS: I do not use the key “networkGamesWon” outside of the gamestatistic class.
PS: It works on linux and android build.

Anyone has an idea what is going wrong?

On game win:

Model::getInstance()->getGameStatistics().increaseNetworkGamesWon();

On stat summary:

GameStatistics().getNetworkGamesWon();

Gamestatistic class:

#ifndef GameStatistics_H_
#define GameStatistics_H_

class GameStatistics {
public:
    GameStatistics(){
        networkGamesWon = CCUserDefault::sharedUserDefault()->getIntegerForKey(
            "networkGamesWon", 0);
    }
    virtual ~GameStatistics();
    int getNetworkGamesWon(){
        return networkGamesWon;
    }
    void increaseNetworkGamesWon() {
        networkGamesWon++;
        CCUserDefault::sharedUserDefault()->setIntegerForKey("networkGamesWon",
                networkGamesWon);
    }
private:
    int networkGamesWon;

};

#endif /* GameStatistics_H_ */

#2

Try calling CCUserDefault::flush method right after setting the new values.


#3

fixed it, thx