Game Development Reference
In-Depth Information
}
Now that you have created this function, you can take a closer look at what is happening
here.
The first thing you do is create a CMMotionManager . This is one of the entry points in-
to the CoreMotion framework. Then you check to make sure you have an accelerometer.
If you don't do this and run this code without the check, you will get a crash whenever
you try to use the manager. Also, some devices such as older iPods don't have accelero-
meters. Remember, your games and applications can run on just about any iOS device.
Now that you know there is an accelerometer, you will want to set the update interval to
be used. In some games, you may want to have the interval be slower or faster. For this
game, you are going to get an update every 1/60th of a second.
Since you are getting updated every 1/60th of a second, you don't want to tie up the main
cycle of the game, so you will create an update queue. This queue will take a closure and
run this function every time the accelerometer update is called.
When the function is called, you create a few variables to store the current location of
your hero. To keep things simple, you will check to see whether the y-axis has changed a
certain amount. A negative change indicates the user has tilted the device right, so you
need to move the hero to the right; a positive change indicates the user has tilted the
device left. Here you are using 0.20; however, if you want a fine-grained adjustment, you
can decrease that number.
Using this information, you simply create an animation for the hero to move a certain dis-
tance.
The only thing left now is to add the setupAccelerometer() call to your
viewDidLoad() method in the GameViewController . Once you have added all
that, it is another good time to run the game and see what you have accomplished.
When you have your hero moving by touching the device, tilt it to the left or right, and
you will see him move in that direction. There are some variables that you can adjust to
have him move faster or slower. You should go back and modify the moveDistance in
the render function to see how this affects the movement. Also, adjust the threshold in
the setupAccelerometer function to see how the device response changes.
Enemy Node
Search WWH ::




Custom Search