Light Speed iOS Apps: Padsteroids 2

In the first phase of the Padsteroids tutorials we created the iPad application and added some simple drawing to it. In phase two we will add interactivity by adding controls for the player to manoeuvre the spaceship around the screen and fire a laser beam which will, in the completed application, be used to destroy asteroids.

The iOS toolkit comes with a rich set of controls, buttons, list boxes, date controls and so on. But this is a game, and for this game we want to have something like joysticks. Sadly there is no native joystick control so we are going to create a new view called ‘CircleControlView’ that acts like a joystick. And we will use two of these controls, one to fire the laser, and the other to move the ship.

Building the CircleControlView

Creating the new CircleControlView class starts with using the ‘New > File’ option on the Padsteroids folder as shown in Figure 1. This is exactly the same process that we used to create the GameSurface view.

--ADVERTISEMENT--

Figure 1

As with the GameSurface view the next steps are to select that we want to create an Objective-C class, and that it should descend from UIView. Then we need to name it CircleControlView as shown in Figure 2.

Figure 2

If you have created the CircleControlView class properly you should see new CircleControlView.h and CircleControlView.m classes added to your project as you can see in Figure 3.

Figure 3

The next step is to dig into the CircleControlView.h file and add both some member variables and two accessors as shown in Listing 1.

The initializedCenter is a Boolean that indicates whether the center has been initialized for the control. And the centerPoint stores the current location of the joystick within the view. The two accessors, getAngle and getDistance return the angle of the joystick from the center in degrees, and the distance from the center as a percentage of the total available distance. For example, the control could be at zero degrees and zero distance, meaning that it’s in the center. Or it could be at 25 degrees at 50% distance, meaning that it’s been moved up and to the right at half way between the center of the control and the perimeter of the circle.

With the definition for the class all set up it’s time to change around the CircleControlView.m class to implement this new interface. The new code for the initialization and drawing of the control is in Listing 2.

The drawing code is the most interesting part. It starts with initializing the center point if this is the first time the control is being drawn. It the uses drawing to draw a filled circle, then the border of the circle, and finally the control ball in it’s current location. Feel free to play with the colors and the sizings and such, part of the fun of developing your own controls is that you have complete control over the look and feel.

The next step is to add the user interactivity to the control using the methods shown in Listing 3.

These are fairly standard UI methods; touchesBegan, touchesMoved and touchesEnd. iOS sends these events as the player touches the control and moves around. When they finish the touch the touchesEnded method is called. In this case we use the touchesMoved method to track the users thumbs as they move the joystick. And the touchesEnded method to return the joystick to the center position. Listing 4 shows the accessor methods for the class.

These methods look tricky, but it’s really just a bunch of math to get the angle and distance from the current control point where the user has placed their thumb to the center of the control. The getDistance method uses they Pythagorean theorem to get the distance and returns it as a percentage. And the getAngle method does a lot of trig work to find out the angle for the given quadrant.

With the controls developed it’s time to update the GameSurface class so that we can move the ship around.

Updating the GameSurface

We will start with simply moving the ship, then move to firing the laser later in this tutorial. To move the ship we need to add a moveShip method as shown in Listing 5.

The code for moveShip changes the location of the ship by altering it’s x and y coordinates by a value calculated from the angle and the distance values passed in. It also stores the current angle of the ship so that when it’s rendered it’s pointing in the right direction.

With those changes made it’s time to add references to the game surface and the two circle controls to the PadsteroidsViewController.h

It’s important that we make that modification now because as we change the XIB file we are going to link the controls and views to the PadsteroidsViewController class.

Updating the PadsteroidsViewController.xib

Most of the coding changes we need to move the ship around have been made, so now it’s time to add the CircleControlViews for motion and fire control to the interface that’s defined in PadsteroidsViewController.xib.

Step one is to open up the XIB file and select the Identity Inspector from the ‘View > Utilities’ menu. Down at the bottom of the panel is the set of available objects to add to the view as shown in Figure 4.

Figure 4

Scroll down a little and you will find the View object. Drag a View object onto the main view and it will appear in the interface. From there, change the Class at the top of the panel from UIView to CircleControlView as shown in Figure 5.

Figure 5

Now we need to size the view appropriately. Move to the Sizing Inspector and set the view to the sizes shown in Figure 6.

Figure 6

Now change over to the Attribute Inspector and change the Mode to Redraw, the Background to Clear Color, and make sure the User Interaction Enabled checkbox is selected as shown in Figure 7.

Figure 7

We need two of these controls, so follow the same process to add another view, change it to CircleControlView, and set it’s sizings to the ones shown in Figure 8.

Figure 8

You also need to set the attributes for this control as shown in Figure 7. Now you should have two controls in the view. One at the lower right, and the other at the lower left. The one on the lower left controls firing the laser. The one on the right is the motion controller.

The next step is to link these to the PadsteroidsViewController which is considered to the be the “File’s Owner”. The first step is to right click on the CircleControlViewer on the left hand side. You should see a popup window as shown in Figure 9.

Figure 9

From there, click on the small ‘O’ at the right of New Referencing Outlet and drag it to the “File’s Owner” placeholder. You should see something like Figure 10.

Figure 10

From there, let go of the mouse button and it should bring up another popup menu that gives the list of the possible connection points as shown in Figure 11.

Figure 11

For the fire control select the fireControl item. Repeat the process for the motion control and connect it to the motionControl point. You also need to connect the game surface to the gameSurface item as shown in Figure 12.

Figure 12

This can be really alien the first time you do it. The Xcode interface builder is unlike any other user interface builder I’ve used in the past. But once you get used to it, it starts to make sense.

Finishing The Code Changes For Motion

The last step to get the motion control going is to make some minor modifications to the PadsteroidsViewController.m file as shown in Listing 8.

What we are doing here is creating a timer in the viewDidLoad function, then responding to that timer in the new gameUpdate method. The gameUpdate method looks at the current position of the motionControl. If the distance is greater than zero it means that the user has moved the control away from the center point indicating that they want to move the ship. It then calls the moveShip method to move the ship around given the angle and distance.

If everything is hooked up properly you should see something like that which is shown in Figure 13 when you launch the application.

Figure 13

And by moving the motion control you should be able to move the ship around as you can see in Figure 14.

Figure 14

Alright! Now we have a game that’s actually starting to feel like a game. But to bring the whole thing to life we need to be able to do something more than move, we need to be able to shoot something.

Adding the Laser

What would a game be without some way to blow stuff up? For that we are going to add a laser to the spaceship and hook that up to the fire control so that the player can point and shoot in whichever direction they like. Listing 9 shows the additions we need to make to the GameSurface.h file.

The additions amount to two new methods enableGun and disableGun. The enableGun method is called when the user has moved the firing joystick with his thumb. And the disableGun is called when the player has released the firing joystick.

Now that we have a way to enable and disable the gun in the interface we have to start making the changes required to add support for it in the class. This starts with modifying the drawRect method as shown in Listing 10.

Here we are adding monitoring for the fireControl view and then sending the enableGun or disableGun message to the game surface as appropriate. If everything goes as planned you should be able to use the left hand stick to fire the laser as shown in Figure 15. This is starting to look and feel like a real game at this point.

Figure 15

Conclusion

There is a lot to learn in this part of our tutorial. You learn about how to handle touches on a view in the interface. As well as how to hook up objects in the interface builder. And also how to create a timer and use it to drive game play.

With the ability to move and fire we have added some interactivity to our little Padsteroids game. The next step, which will be in the final article of the tutorial, will be to dynamically add some asteroid views to the game surface, move them around in random directions with random velocities, and the build a collision detection system so the laser can blast them out of existence.

Jack Herrington is an engineer, author, and presenter who lives and works in the San Francisco Bay Area. He lives with his wife, daughter and two adopted dogs. When he's not writing software, books, or articles you can find him cycling, running, or in the pool training for triathlons. You can keep up with Jack's work and his writing at http://jackherrington.com.