How can make a global variable available through my game (like saving some game state) From below code itseems I need to declare in the Appdelegate.m but how then can I access it in my class instances??
thanks
//////////////////////////* code I’d like to reuse?
/ global variables
int gLevel;
int gScore;
in my app delegate:
// if this is the first time ever running
// this app we need some default defaults
// we are starting up
// get the score and level
// that we saved when we last quit
// or if its the first time ever running this game
// it will load the default defaults
CCUserDefault help you to figure out where’s the writable folder on each platform, so you don’t need to care about where it writes to & reads from.
For global usage, my favorite approach is to write a singleton class, and include the class header in other cpp files, call MySettings::getInstance()->writeSomething(…) when I need. You can refer to static SimpleAudioEngine* SimpleAudioEngine::sharedEngine() function for a sample.
thanks. what ’s where I have a problem understanding at which stage that singleton class gets instanced? I need to create a an instance in the Appdelegate oninit and then release it onexit?
I will look up simpleaudioengine for the syntax, but I must confess it’s hard to follow e.g. where the shared director and sharedEngine gets created and why he is accessible throughout the life of the app…
thanks for the great support on my 4 other questions
found with google… it’s created the first time the getinstance is called but some precaution is required to … next question is then why shared director and shared engine are not called via getinstance?
It may be worth in the documentation to mention all the singletons: sharedDirector, sharedEngine, sharedUserDefault (any other) and to mention that cocos2dx already make them available…for new users it appears like “magic”…
2- since CCUserDefault::sharedUserDefault belongs to cocos2dx, then I can’t init it in Appdelegate i can only access it in my first scene I create?
3-Do you know in Xcode wheter the tiny database is kept between several runs meaning whether I can test if a data was properly written for the next launch of the application? or I need to test directly with the iphone?