AS3: Main
Aha, my first AS3 tutorial! Let's begin!
Here's what you will be learning how to do using AS3:
Example
After struggling to find a way to make object movement using AS3 without the use of deltas code (which I refused to use =p) I finally found a simple way to do it, that I'd like to share in tutorial form, to help others understand.
So, first, open up a new AS3.0 flash document, and open the actions for the first frame.
The first thing you want to do, is define some variables for your arrow keys (up, down, left, right). So using strict-data typing (making things more specific for faster running), we set up our variables:
var varRight:Boolean = false;
var varLeft:Boolean = false;
var varUp:Boolean = false;
var varDown:Boolean = false;
So what we just did was defined 4 variables, eached named varArrowKeyHere
What a boolean is is a true/false argument. you can easily take out the :Boolean part, but it helps to run things even faster and make for less confusion in the Flash scripting.
The next thing we want to do is define some variables for the objects movement speed, like so:
var xspeed:Number = 5
var yspeed:Number = 5
xpseed and yspeed are not the only names you can use. You can call it whatever you'd like. Call them pickleLegs for all I care. The ":Number" is the same type of thing as :Boolean except it applies to numbers and not true/false.
Alright, so now our variables are done, horrah! Now for the harder part.
In AS3, almost everything as far as I know is done using functions. Functions are written the same way as in AS2 or 1 with a little extra bit added, depending on what you do with it.
So, what we want to do now is make a function that checks what keys are down. To do this, we just give the function a name and define what type of function it is. Observe:
function checkKeys(event:KeyboardEvent) {
This is the opening of our function. It is a function that I called "checkKeys" and it is a KeyboardEvent function, meaning it only works with interaction of the keyboard, which will be specifically defined later.
Now we can begin adding actions into the function, and complete it.
So, the easy way to check if a key is being pressed is using a method that involves the keyCode action. To do this, all you do is event.keyCode = #. What goes in # is up to you. You have to know what #'s equal what on the keyboard. In our case, we will be using 37, 38, 39, 40.
37 = left arrow key
38 = up arrow key
39 = right arrow key
40 = up arrow key.
So, what we want to do is make an if statement that checks if one of the above arrow keys is being pressed. Let's start with the right arrow key.
if (event.keyCode == 39) {
trace("Right key is down");
varRight = true;
}
What is happening in this bit of code is flash is checking to see if the right arrow key WAS pressed. Note the was. This method does NOT work like the Key.isDown() method used in AS2 or AS1, hence my struggle to work out this code ;)
Anyway, this method works as a variable, stating that if the arrow key is pressed, it "stays" pressed, in effect. But let's move on for now. Once it detects that he right arrow key (39) is pressed, it will give an output in the testing of the fla stating that the Right key is down. (NOTE: YOU CANNOT TEST THE FLA YET, YOU WILL GET ERRORS) Then, it defines the varRight variable as true. (The boolean is now true).
And now, just paste this code with the proper changes for the remaining 3 keys left.
if (event.keyCode == 38) {
trace("Up key is down");
varUp = true;
}
if (event.keyCode == 37) {
trace("Left key is down");
varLeft = true;
}
if (event.keyCode == 40) {
trace("Down key is down");
varDown = true;
}
Now, all that's left is to end the function, and close it with a }
Your function should now look like this:
function checkKeys(event:KeyboardEvent){
if (event.keyCode == 39) {
trace("Right key is down");
varRight = true;
}
if (event.keyCode == 38) {
trace("Up key is down");
varUp = true;
}
if (event.keyCode == 37) {
trace("Left key is down");
varLeft = true;
}
if (event.keyCode == 40) {
trace("Down key is down");
varDown = true;
}
}
So now that that's out of the way, we now have to have a function that makes the variables false if the key isn't pressed. To do this, we make another function, with the same method of coding, with a different name. Like so:
function keyUps(event:KeyboardEvent) {
Okay, easy, let's move on now. The next set of if statements work pretty much the same as before.
if (event.keyCode == 39) {
event.keyCode = 0;
varRight=false;
trace("Right key is NOT down");
}
That is saying that if the right arrow key is "stayed" as down, then the right arrow is not down and the variable varRight is false.
So now we just add the other 3 if statements and close the function like before. The function should now look like:
function keyUps(event:KeyboardEvent) {
if (event.keyCode == 39) {
event.keyCode = 0;
varRight=false;
trace("Right key is NOT down");
}
if (event.keyCode == 38) {
event.keyCode = 0;
varUp=false;
trace("Up key is NOT down");
}
if (event.keyCode == 37) {
event.keyCode = 0;
varLeft=false;
trace("Left key is NOT down");
}
if (event.keyCode == 40) {
event.keyCode = 0;
varDown=false;
trace("Down key is NOT down");
}
}
Alright, almost done! Now for the actual allowing of the movement.
Make a movie clip on the document and give it the instance name "player".
Create another function, this time instead of event:KeyboardEvent we simply put Event, for now it is a regular event that can be called with the function from anywhere.
So let's make our next function that I will call movement. This function is pretty self explanitory, so I won't bother going over it.
function movement(Event){
if (varRight == true) {
player.x += xspeed;
}
if (varUp == true) {
player.y -= yspeed;
}
if (varLeft == true) {
player.x -= xspeed;
}
if (varDown == true) {
player.y += yspeed;
}
}
There, fairly simple right? Now for the final step!
In AS3, to call functions and make things work, we have to use a method called addEventListener(). This isn't so hard to figure out, but it can be confusing for some...
Basically, we made 3 functions, so it's only logical that we have 3 "eventListeners" that we must "add".
The next 3 lines of code will look like this (following all the names I gave everything):
stage.addEventListener(Event.ENTER_FRAME, movement);
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
All these lines are doing is telling the stage (_root) to add an event listener with the specific event based upon what the function is, and then the given function.
Take the first line for example:
stage.addEventListener(Event.ENTER_FRAME, movement);
This tells the stage to add an event listener with the event lableed ENTER_FRAME (derived from AS2 and 1). Then, with a comma for separation, it adds the function named movement. So it's telling flash to call up the movement function, and have it work in an enter_frame event (think of enter frame as "every frame" which would be based on the frame rate).
And the same goes for the other events. So here's what the final code should look like:
Code (open in notepad or something)
And that's that! I hope it works for everyone and people use it :)