Wednesday, October 13, 2010

Object Oriented Programming using Actionscript 3: Part 5

Today we will create a simple character walking script using external class.

This tutorial will show you how to use classes on your movieclips in your game or application. This is actually very useful to know and makes it simple to implement - simply drag the MovieClip from your library to stage (for example, your main game character) and it will work, because we've defined its class path.

This can also be done to multiple objects - for example, you create an enemy class once, and then apply it to one MovieClip, which you then can copy/paste on the stage as much as you want.

Anyway, let's get started. Here's what we will do:


Nothing complex - just simple hero movement with arrow keys.

First of all, draw your character (you will want it to face bottom) and convert it to MovieClip - hit F8, enter your name, select MovieClip and - now the most important part - set the class path (you will need to check the Export for Actionscript box to be able to enter path).



I've set the path to cl.kirill.myChar, but I do not have that class yet. So i go to the cl>kirill folder and create a new AS3 document. Here's the code, pretty simple and understandable:

package cl.kirill
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;

public class myChar extends MovieClip
{
private var isUp:Boolean;
private var isDown:Boolean;
private var isRight:Boolean;
private var isLeft:Boolean;

public function myChar()
{
trace("Character added.");
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
stage.addEventListener(KeyboardEvent.KEY_UP, upKey);
stage.addEventListener(Event.ENTER_FRAME, movement);
}

public function downKey(evt:KeyboardEvent):void
{
if (evt.keyCode == 39)
{
isRight = true;
}
if (evt.keyCode == 37)
{
isLeft = true;
}
if (evt.keyCode == 38)
{
isUp = true;
}
if (evt.keyCode == 40)
{
isDown = true;
}
}

public function upKey(evt:KeyboardEvent):void
{
if (evt.keyCode == 39)
{
isRight = false;
}
if (evt.keyCode == 37)
{
isLeft = false;
}
if (evt.keyCode == 38)
{
isUp = false;
}
if (evt.keyCode == 40)
{
isDown = false;
}
}

private function movement(Event):void
{
if (isUp == true)
{
this.y-=5
this.rotation=180
}
if (isDown == true)
{
this.y+=5
this.rotation=0
}
if (isRight == true)
{
this.x+=5
this.rotation=270
}
if (isLeft == true)
{
this.x-=5
this.rotation=90
}

}

}
}

Now, if you have the MovieClip of the hero on stage and test your movie, it will work. It will also work if you make 3 copies of the same character and paste them around on stage.

Thanks for reading!

Related:

Object Oriented Programming using ActionScript 3: Part 1
Object Oriented Programming using ActionScript 3: Part 2
Object Oriented Programming using ActionScript 3: Part 3
Object Oriented Programming using ActionScript 3: Part 4

2 comments:

Anonymous said...

thanks for great tutorials please add Interfaces, polymorphism, encapsulation and namespace topics as well thanks

Anonymous said...

thanks for great tutorials please add Interfaces, polymorphism, encapsulation and namespace topics as well thanks

Post a Comment