Moving a game character with the arrow keys is a very common method of player control in flash games. It allows for close control over the game character and gives sort of a mechanical feel to the characters movements.
I am going to show you how to control your game character with the arrow keys, using Actionscript 2.0 and object-oriented programming. No actionscript will go in the .fla file.
First, open an AS 2 flash file: File> New> Flash File (Actionscript 2.0) and create your game character image. Select your game character and go to Modify> Convert to Symbol.
Name it Player (or whatever you want) select Movie clip as the type, under Linkage check the box for export for actionscript and make sure you put Player as the Identifier and the Class. Click okay and you are back to the stage.
Now create a new actionscript file: File> New> Actionscript File. Be sure to save this actionscript file in the same folder as your .fla file.
In this file we will define the class that controls our game character movie clip, Player.
Here is the code that will go in our actionscript file and the explanation will follow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Player extends MovieClip { var velocity; function onLoad() { velocity = 10; } function onEnterFrame() { if( Key.isDown(Key.RIGHT) ){_x = _x + velocity;} if( Key.isDown(Key.LEFT) ){_x = _x - velocity;} if( Key.isDown(Key.UP) ){_y = _y - velocity;} if( Key.isDown(Key.DOWN) ){_y = _y + velocity;} } } |
Line 1-2: We specify the class that will control our movie clip
Line 4: We define velocity as the variable that will contain the speed at which our game character moves when an arrow is pressed.
Line 6-9: In function onLoad() we set our velocity equal to 10 to specify the speed.
Line 11-20: Under function onEnterFrame() we define what happens when each of the arrow keys are pressed. The left and right arrows move our game character along the x axis so our velocity is either added or subtracted from our current _x position. The up and down arrow keys move the player along the y axis so we add or subtract our velocity from our current _y position to get our new position on the y axis.
Line 22: Closing curly bracket for the class
Make sure you have the appropriate curly brackets or the code will not function correctly.
That’s it. The notation is the most difficult part.
This code is an integral piece for building a flash game with arrow key controls, using object-oriented programming.
Here is our game character moving with the arrow keys: (click on image before attempting movement)
hmmm.. this doesn’t seem to work… i saved my player image in the same folder as the script and everything else seems to be as it should. although when i test the movie in the cs3,it says in the “Compiler errors” box – “the class or interface ‘player’ could not be loaded”. any help?
when I test the movie it says “the class or interface ‘player’ could not be loaded”
any help?
Are you sure you set the class properly in the properties? Capitalization matters. Go to your library, right click on the Player symbol and go to properties. Make sure your class name is exactly the same as what you have there. You may have named your class file ‘Player’ but set the class as ‘player’ in the properties.
Also, make sure your Player.as file is in the same folder as your .fla.
ok, i reset the properties and it’s worked! thanks and keep up the good work!
Hi. Do you have any tutorials on collision? Like if the cat head was to go through a ‘maze’ to the edge of the screen. Also, creating a trigger point if the cat, say, gets to the edge of the screen and it would trigger another screen?
I still use mx2004 so I just removed the Class and used an onClipEvent for the code that I added to my MC actions. It worked in your example above and hopefully I can find some collision and/or trigger script tutorials.
Thanks much!
I don’t have any tutorials on collision. Sorry!