re-init a view controller...from within

Member
Posts: 60
Joined: 2009.06
Post: #1
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?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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?
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #3
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.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2009.02
Post: #4
You can always get a handle to your main AppDelegate by doing this:

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
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #5
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.
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #6
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...
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #7
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.
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #8
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. Blush
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #9
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Making superview's controller respond to events vangelisfx 2 2,822 Jan 19, 2010 04:47 PM
Last Post: vangelisfx
  Model-View-Controller and touch events Vinnie 3 3,486 Jun 3, 2009 09:27 AM
Last Post: AnotherJake
  question on init behavior kendric 3 2,332 Mar 27, 2009 07:55 AM
Last Post: kendric