Introduction to AS3 classes
In my opinion one of the most important reasons people are afraid to migrate from AS2 to AS3 is its way of scripting.
Pure AS3 coding dislikes coding on the timeline and wants us to write and use custom classes.
Don’t worry: you can still write your script on the timeline but you won’t be able to benefit of new AS3 features doing this way.
If you want to buy a Flash AS3 version and keep on writing on the timeline, then simply don’t buy it.
You will find it’s not that hard to learn AS3 classes, but one common mistake is keeping the entire code into a single class.
If you aren’t doing this for an educational purpose (it’s easier to explain the code when it’s all on a single file), then you should write as many classes as you can.
The reason is simple: the code is cleaner and easier to read.
Moreover once you wrote a messy class you won’t be able to use it again in a future project, but if you have a collection of small classes, there are good chances to use them again and again and again, saving a lot of time and work.
This is a “wrong” class that simply traces if I pressed up, down, left, right or space keys.
I’ve taken it from New tile based platform engine – AS3 version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package {
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class all_in_one extends Sprite {
var press_left = false;
var press_right = false;
var press_up = false;
var press_down = false;
var press_space = false;
public function all_in_one() {
addEventListener(Event.ENTER_FRAME,on_enter_frame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
public function key_down(event:KeyboardEvent) {
if (event.keyCode == 32) {
press_space = true;
}
if (event.keyCode == 37) {
press_left = true;
}
if (event.keyCode == 38) {
press_up = true;
}
if (event.keyCode == 39) {
press_right = true;
}
if (event.keyCode == 40) {
press_down = true;
}
}
public function key_up(event:KeyboardEvent) {
if (event.keyCode == 32) {
press_space = false;
}
if (event.keyCode == 37) {
press_left = false;
}
if (event.keyCode == 38) {
press_up = false;
}
if (event.keyCode == 39) {
press_right = false;
}
if (event.keyCode == 40) {
press_down = false;
}
}
public function on_enter_frame(event:Event) {
if (press_left) {
trace("left");
}
if (press_right) {
trace("right");
}
if (press_up) {
trace("up");
}
if (press_down) {
trace("down");
}
if (press_space) {
trace("space");
}
}
}
} |
As you can notice, it’s not that easy to use the code that checks for key pressed because is made by lines 7-11, lines 14-15 and lines 17-50.
Obviously you can cut/paste them but it’s easy to forget some instructions and make errors.
If you want to write good code, you should make a class looking for pressed buttons and use it from the main file.
This is an example of the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package {
import flash.events.KeyboardEvent;
public class keys {
private var press_left = false;
private var press_right = false;
private var press_up = false;
private var press_down = false;
private var press_space = false;
public function keys(movieclip) {
movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
movieclip.stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
public function is_left() {
return press_left;
}
public function is_right() {
return press_right;
}
public function is_up() {
return press_up;
}
public function is_down() {
return press_down;
}
public function is_space() {
return press_space;
}
public function key_down(event:KeyboardEvent) {
if (event.keyCode == 32) {
press_space = true;
}
if (event.keyCode == 37) {
press_left = true;
}
if (event.keyCode == 38) {
press_up = true;
}
if (event.keyCode == 39) {
press_right = true;
}
if (event.keyCode == 40) {
press_down = true;
}
}
public function key_up(event:KeyboardEvent) {
if (event.keyCode == 32) {
press_space = false;
}
if (event.keyCode == 37) {
press_left = false;
}
if (event.keyCode == 38) {
press_up = false;
}
if (event.keyCode == 39) {
press_right = false;
}
if (event.keyCode == 40) {
press_down = false;
}
}
}
} |
To use it, you just have to save it as keys.as in the same path of the main file, that at this time you will change this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package {
import flash.display.Sprite;
import flash.events.Event;
public class all_in_one extends Sprite {
public function all_in_one() {
var my_sprite = new Sprite();
addChild(my_sprite);
my_input = new keys(my_sprite);
stage.addEventListener(Event.ENTER_FRAME,on_enter_frame);
}
public function on_enter_frame(event:Event) {
if (my_input.is_left()) {
trace("left");
}
if (my_input.is_right()) {
trace("right");
}
if (my_input.is_up()) {
trace("up");
}
if (my_input.is_down()) {
trace("down");
}
if (my_input.is_space()) {
trace("space");
}
}
}
} |
Notice at line 6 and 7 I created a sprite, and at line 8 I declared a keys object (the same name as the class file).
Then at lines 12, 15, 18, 21 and 24 I called class functions such as is_down, is_space and so on.
Last but not least, I have a class I can use in every platform game project.
They can be easily customized to meet the unique requirements of your project.















(16 votes, average: 4.75 out of 5)









This post has 11 comments
Michael W
Hey. Im getting 1120: Access of undefined property my_input 6 times. I have the first class saved as keys.as and the second as doc.as, which i set as the document class on my empty fla. All files in the same directory. Im trying to get the basics of class oriented as3 but can’t seem to figure out when to do what, ie set something as document class or attach it via linkage.. Could you give me a pointer as to what is wrong?
Prankard
The code doesn’t work for me. Might be because I’ve gotten strict mode turned on in CS3 (which is on by default).
That’s in File>Publish Settings>Actionscript 3.0 Settings>Strict Mode.
Basically you forgot to declare your my_input variable and my Output throws errors whenever you try to access it.
Add:
private var my_input:keys;
below the “public class all_in_one extends Sprite”
And then it works.
Michael W
Yeah, that solved it, thanks man! ;-)
Anton
Hello! I’m from Russia. Sorry for my poor english.
It’s a very good example. Thank you.
But I did not understand one thing. Why did you use the functions like keys.is_up, keys.is_down and so on?
I think that it is easier to use just properties of the keys class like keys.press_up, keys.press_down etc… (of course, you should before change the access modifiers from private to public for these properties)
Maybe I’m wrong…
tasos
Hello,
On that part of the code:
public function keys(movieclip)
{
movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down); movieclip.stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
why are you using a movieclip to access the stage?
couldn’t you do something like
stage.addEventListener(KeyboardEvent.KEY_UP, key_up)
ommiting the movieclip object?
Thank you!
Anton
Tasos, as i understand you can use the Stage class only as a property of DisplayObject class. It is not globally accessible.
To use your expression (stage.addEventListener…) keys class should Extend any DisplayObject class (Sprite for example).
Author made an universal class keys that can be used anywhere in platform games, so he did not use here any excess things.
Joe Sacher
I’m new to flash and learning AS3, but experienced in other languages. This keys.as seems great if you are only ever using arrow and space. Expanding, however is problematic. Should there be a method for each key?
Why not:
package
{
import flash.events.KeyboardEvent;
public class keys
{
public const LEFT_ARROW:int = 37;
public const RIGHT_ARROW:int = 39;
public const UP_ARROW:int = 38;
public const DOWN_ARROW:int = 40;
public const SPACE_BAR:int = 32;
public var keysTracked:Array;
public function keys(movieclip)
{
keysTracked = new Array();
movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
movieclip.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
public function trackKey(keyCode:int):void
{
keysTracked[keyCode] = false;
}
public function unTrackKey(keyCode:int):void
{
keysTracked[keyCode] = undefined;
}
public function isKeyDown(keyCode:int)
{
return keysTracked[keyCode];
}
function keyDownHandler(event:KeyboardEvent)
{
if(keysTracked[event.keyCode] != undefined)
{
keysTracked[event.keyCode] = true;
}
}
function keyUpHandler(event:KeyboardEvent)
{
if(keysTracked[event.keyCode] != undefined)
{
keysTracked[event.keyCode] = false;
}
}
}
}
To add a key, add a constant for it. Or you can even call this with an integer, instead of the constants if you really want.
myInput = new keys(keySprite);
myInput.trackKey(myInput.UP_ARROW);
myInput.trackKey(myInput.DOWN_ARROW);
myInput.trackKey(myInput.LEFT_ARROW);
myInput.trackKey(myInput.RIGHT_ARROW);
myInput.trackKey(myInput.SPACE_BAR);
Test with:
myInput.isKeyDown(myInput.UP_ARROW);
Kev Man
Whatever you do, dont start with something simple and build up.
By all means, jump right in and overload us with code. Make sure you dont break it down and explain the 4 main parts.
Oh, and dont follow up with a how to implement this into your flash.
Noobs hate it when you give them the information they need and not confuse them with a bunch of other stuff.
Paul
It is COMPLETELY beyond my understanding how ANYONE can actually claim that this AS3 is superior to AS2. With all these Classes and Methods and what not yesterday it took me 45 lines of AS3 code to write something which started with a single button onstage and at the click of this button moved it off stage and placed 6 other buttons on stage. In AS 2 it would have taken only 2 timeline frames and a single line of code to achieve the EXACT same effect.
Now my problem is that the above needs to be inserted into the beginning of another project as the second project will be accessible from any one of the 6 buttons now onstage. Now the above has its own Main.as and obviously the second project also has its own Main.as . Now how the HECK do I combine the two. Even the code in this tutorial throws up all kinds of errors.
Sorry if I sound frustrated. I dont want to learn this AS3 but I guess I will have to or get left behind.
Paul
Taizyd
I these AS3 platform engine blogs had comments explaining how it all worked
Danny Phantom
@Paul.
Better Management of Code -
The Final Product Runs Faster Than AS2.
The better it gets the more complex it is to implement it. AS2 is by no means better than AS3 and I can surely say that AS3 is better than AS2.
It takes less code in AS2 but you get less performance too. So keep that in mind.