Game Development Reference
In-Depth Information
The viewDidLoad tasks, as shown in Listing 3-2, is called when an instance of GameController is
loaded from an XIB file. Because an instance of GameController is in each MainWindow XIB file, this
happens as soon as the application starts. After calling the super implementation of viewDidLoad ,
which is required, we add the UIView loadingView to the main view of the GameController class. The
task addSubview: displays the new sub-view at the location and size as defined by the sub-views
frame property. Because loadingView is defined in the XIB file, its frame is defined there.
After we get the Loading view on the screen, we want to load any initial resources so we can prevent
an untimed pause later in the application life cycle. It is not technically required in this app to load
the images asynchronously, but if we wanted to display a progress bar, this would be required. In
order to perform a task asynchronously, we simply call performSelectorInBackground:withObject:
and pass in the task loadImages . The task loadImage will then be run in the background; it is shown
in Listing 3-3.
GameController.m (loadImage)
//sleeping so we can see the loading view. In a production app, don't sleep :)
[NSThread sleepForTimeInterval:1.0];
[coinsController loadImages];
[loadingView removeFromSuperview];
[self.view addSubview:welcomeView];
loadImage task calls sleepForTimeInterval and causes the current thread to sleep for a second.
This is done to make sure you can actually see the loading view in the sample code; delete anything
like this in production code. After the sleep, call loadImages on coinsController . The details of
CoinsController 's implementation of loadImages is described in Chapter 4—all you need to know
now is that it loads all of the images used to display the coins. When we are done loading, we
remove the loadingView and add the welcomeView .
The Welcome view is really where the application starts for the user. From there, the user can access
the interesting parts of the application.
Changing Views in Response to User Actions
Once the Welcome view is displayed, the user is presented with a number of buttons (this view
is shown in Figure 3-3 ). The top button, Continue Game, is only present if the user was partially
through a game when they exited the application. Each button causes the user to navigate to
a new view. This is done by first establishing a task that will be called when the user touches a
button. In Listing 3-1, we see a number of tasks declared in the GameController.h file. The ones
starting with IBAction are tasks that are called when a button is touched. Once the tasks are
declared in the header file, you can wire up each button to each task. Figure 3-9 shows the first
step in wiring up a button.
 
Search WWH ::




Custom Search