re-init a view controller...from within
My game has a view controller for the main menu and a view controller for the gameplay.
I initialize the gameplay view controller at the touch of a button in the main menu and the add its view as a subview. This has been working fine so far.
However, I'd like to have a button in the gameplay menu that restarts the level.
I tried calling the custom init method on self from within the game view controller but it didn't do anything!
How do I regen the game view controller and its view from within its own .m file?
I initialize the gameplay view controller at the touch of a button in the main menu and the add its view as a subview. This has been working fine so far.
However, I'd like to have a button in the gameplay menu that restarts the level.
I tried calling the custom init method on self from within the game view controller but it didn't do anything!
How do I regen the game view controller and its view from within its own .m file?
It sounds like you need to rearchitect your code a bit. You shouldn't be keeping so much state in your view controller that you have to reinitialize it to restart your level; ideally, it would be in a model that you could simply swap out for a newly created one to restart. Read about MVC for help on knowing which responsibility goes where.
That said, there's no reason [self init]; wouldn't do anything. You're not doing something like self = [[MyClass alloc] init];, right?
That said, there's no reason [self init]; wouldn't do anything. You're not doing something like self = [[MyClass alloc] init];, right?
ThemsAllTook Wrote:It sounds like you need to rearchitect your code a bit. You shouldn't be keeping so much state in your view controller that you have to reinitialize it to restart your level; ideally, it would be in a model that you could simply swap out for a newly created one to restart. Read about MVC for help on knowing which responsibility goes where.
That said, there's no reason [self init]; wouldn't do anything. You're not doing something like self = [[MyClass alloc] init];, right?
Thanks...I'm in an advanced stage now, otherwise I would have probably started with Cocos 2D...I'll download the code and see if it has game level transitions. I'm really stumped.
You can always get a handle to your main AppDelegate by doing this:
then from within you gameViewController you can call something like
The restartGame method could clean up the current gameViewController, release it, and alloc/ init a new one.
We do do something like this in most of our games. You simply need to keep a reference to your gameViewController from your AppDelegate so that the restart method there can clean it up.
So that is one option. The other option is to create a method inside of your gameViewController that is able to clean things up, reset all of your game state, and start things over. Sometimes this option isn't easy, and the first option is simpler. However, if you've loaded a lot of assets and other game stuff, having to tear it all down and re-init can cause slow load times and frustrations for your users.
Hope that helps!
-Keith
Code:
UIApplication *theApp = [UIApplication sharedApplication];
MyMainAppDelegate *theAppDelegate = (MyMainAppDelegate *)theApp.delegate;then from within you gameViewController you can call something like
Code:
[MyMainAppDelegate restartGame];The restartGame method could clean up the current gameViewController, release it, and alloc/ init a new one.
We do do something like this in most of our games. You simply need to keep a reference to your gameViewController from your AppDelegate so that the restart method there can clean it up.
So that is one option. The other option is to create a method inside of your gameViewController that is able to clean things up, reset all of your game state, and start things over. Sometimes this option isn't easy, and the first option is simpler. However, if you've loaded a lot of assets and other game stuff, having to tear it all down and re-init can cause slow load times and frustrations for your users.
Hope that helps!
-Keith
thanks keith, I've heard of the app delegate before, for some reason I discounted it...but it really looks tlike the right way to tackle this. i shall attempt to implement your code.
hmm, I want to make my life even easier...
I have a view controller for the main menu view, and the game view controller is a property of that class. How can I call a main view controller method to reinitialize the game view controller, again, from within game view controller...
I can't be the only one who has the game set up this way...
I have a view controller for the main menu view, and the game view controller is a property of that class. How can I call a main view controller method to reinitialize the game view controller, again, from within game view controller...
I can't be the only one who has the game set up this way...
On viewWillDisappear:, you should free up any resources you won't be using when a view controller's view is not on the screen.
On viewWillAppear:, you should initialize any state variables for a view controller.
You should also read up on MVC, as Alex suggested. The problem you are having is not a code problem, it is a experience and logic problem.
On viewWillAppear:, you should initialize any state variables for a view controller.
You should also read up on MVC, as Alex suggested. The problem you are having is not a code problem, it is a experience and logic problem.
believe it or not, I haven't found any good tutorials - easily accessible but in-depth, with code samples, etc, on the MVC and its use for games. The only comprehensive game programming tutorial I found was the iTennis one at icodeblog.com
OTOH, on this forum I have read many posts dissing the MVC....
So please forgive me, I am a little lost.
OTOH, on this forum I have read many posts dissing the MVC....
So please forgive me, I am a little lost.
I figured out how to restart my level upon pushing a button in the game view controller itself. Simply calling the init method on self works perfectly. I have all the initial conditions hard-coded in there anyway.
This approach works well for my game which has relatively few variables to initialize.
This approach works well for my game which has relatively few variables to initialize.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Making superview's controller respond to events | vangelisfx | 2 | 2,601 |
Jan 19, 2010 04:47 PM Last Post: vangelisfx |
|
| Model-View-Controller and touch events | Vinnie | 3 | 3,226 |
Jun 3, 2009 09:27 AM Last Post: AnotherJake |
|
| question on init behavior | kendric | 3 | 2,156 |
Mar 27, 2009 07:55 AM Last Post: kendric |
|

