Discussion for Tutorial 7 - Singleton Class

Discussions for all the iPhone Screencast Tutorials.

Discussion for Tutorial 7 - Singleton Class

Postby jonny » Sun Aug 23, 2009 3:16 am

This is a small tutorial on how to create and use a Singleton class. Whilst I’ve been creating these tutorials it has become clear that managing state is important in your game. The ability to manage both the OpenGL state locally and also any state related to your game is a key building block for a game.
jonny
Site Admin
 
Posts: 11
Joined: Wed Apr 22, 2009 4:50 am

Re: Discussion for Tutorial 7 - Singleton Class

Postby Renan » Fri Aug 13, 2010 10:50 pm

Mike,

I have been working with multiple scenes as i mentioned in the TileMap section and something that is really bugging me is being able to use the same player instance in every scene. The player itself is allocated in the GameScene method initNewGameState, but when you use more than one scene accessing that instance player made me put some weird code together in order to work:

// Instance of a scene.
GameScene *oldScene;

// The scene i just came from.
oldScene = [sharedGameController.gameScenes objectForKey:@"game"];

// This player is declared in the .h of the scene i am going to as Player *player but is not allocated again. The new player gets the "old" player and can manipulate the original afterwards.
player = oldScene.player;

This was the way i found to pass the player from one scene to another. It worked so far never had any bug going in and out of the scenes but feels like i am cheating the code or something. Is this the right way to do this? Should i invoke the singleton in any way?

Renan.
Renan
 
Posts: 6
Joined: Wed Mar 24, 2010 1:39 pm

Re: Discussion for Tutorial 7 - Singleton Class

Postby Sumaleth » Sat Aug 14, 2010 4:12 am

A singleton is one way to do it.

Another approach would be to have a reference to the player object in the Director singleton. Or even create it in Director, and give a reference to each Scene as they're created.

I don't know if either approach is more correct or faster.

Rowan.
Sumaleth
 
Posts: 123
Joined: Thu Sep 03, 2009 9:55 am


Return to iPhone Game Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron