Paskaler
Paskaler

Reputation: 1

How to transform one object to another in Objective-C?

I am working on a small game which is pretty much finished. The thing is it didn't have any menus or game over acreens and the like. Everything used to be in a single class Game which had all the logic and rendering. To make the menus possible I created a State class and now there is a StateGame, StateMenu, ... The problem is that I don't know how to switch one state to the other. Currently the State class has a method switchTo: (State*)new state. In the method I put:

[self release];
self = newState;

This produces a segfault and I changed it to:

State *old = self;
self = newState;
AUTORELEASE (old)

This simply doesn't work.

Sorry for the formatting but I'm typing this on a phone, anyways, any help would be greatly appreciated

Upvotes: 0

Views: 67

Answers (1)

Florian Burel
Florian Burel

Reputation: 3456

Well, you probably don't want to "change the object to another"... (Not recommended by the way, self = ... bad idea :[ )

What you'll want is a new Class called, ie GameManager. it will have a property that can be Game, a Menu or a GameOver instance. It will work as a state machine (perform action on state changes) and will display, according to the state, a Game, a Menu or a GameOver screen.

Upvotes: 1

Related Questions