02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

17 Platformers<br />

Creating a platform-based game is quite a challenge, but by this stage in the book you should<br />

have acquired all the necessary skills. In this chapter we are going <strong>to</strong> look at the problems of<br />

controlling the behaviour of a sprite in a platform world. First we will look at an example in which<br />

we move the sprite whilst the background remains stationary and then we will look at using a<br />

multi-layered scrolling background. Platform games often use dynamically created sprites; we will<br />

look at creating sprites on the fly and removing them when they are no longer in use.<br />

The basics of platform games<br />

Platform games involve two fundamental concepts. Firstly, user control of a sprite character that<br />

can usually perform several actions: walk, run, jump fall etc. Secondly, collision detection <strong>to</strong><br />

ensure that the character remains anchored <strong>to</strong> the world that is presented <strong>to</strong> the player. Both parts<br />

of creating a platform game need careful coding <strong>to</strong> ensure success. You may prefer <strong>to</strong> start with<br />

the collision testing or with the user control, but either way they are two separate problems and<br />

should be tested and debugged separately as far as possible. Here we will start with the problem of<br />

user control.<br />

Responding <strong>to</strong> user input<br />

Usually the only control the user has over the gameplay will be via a few key presses or use of the<br />

mouse. Often the control is context sensitive. For example, if a character is falling then the player<br />

cannot set a new direction until the character has landed and the player regains control. For this<br />

reason always use a control flag. If the character is under player control then set the flag <strong>to</strong> ‘true’<br />

and if the character is under computer control then set the control flag <strong>to</strong> ‘false’. Because wherever<br />

possible we want <strong>to</strong> keep the data pertaining <strong>to</strong> a movie clip within that movie clip, place this flag<br />

as a variable inside the clip. The actions a sprite can perform are often limited both by its placement<br />

on screen and the action that is currently being performed, so for this reason use a variable that<br />

s<strong>to</strong>res the current action. Responding <strong>to</strong> keyboard input is easier <strong>to</strong> handle than mouse input for a<br />

platform game. Usually keyboard control will mean reading the arrow keys, space bar and possibly<br />

the control and shift keys. Reading the keyboard is easy using the ‘Key.isDown( )’ construct. To<br />

clarify the principles let’s look at an example. Open ‘Examples/Chapter17/FatCat01.fla’. Press<br />

‘Ctrl + Enter’ <strong>to</strong> run the program. Using the arrow keys you can move the cat around the screen,<br />

jumping and landing on the various platforms that you can see on the screen.<br />

260

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!