Thursday 27 March 2008

creating a platform game in as3 part 2

Read creating a platform game in as3 if you haven't already.


In this tutorial i will show you how to make your character move and jump.

So... we need to make a function for key up events and for key down events to monitor key input so that we can have player controlled characters.

lets put this into code



import flash.display.*;
import flash.events.*;
stage.frameRate = 30;
stage.addEventListener(Event.ENTER_FRAME, run);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);

var gravity:Number = 0.2;
var velocity:Number = 0;
var lspeed:int = -2;
var rspeed:int = 2;
var goleft:Boolean = false;
var goright:Boolean=false;
var attemptJump:Boolean=false;
var char:MovieClip = new MovieClip();
char.graphics.lineStyle(2, 0x112233);
char.graphics.beginFill(0x112233);
char.graphics.drawRect(200, 0, 20, 50);
char.graphics.endFill();
addChild(char);

var land:MovieClip = new MovieClip();
land.graphics.lineStyle(2, 0x112233);
land.graphics.beginFill(0x112233);
land.graphics.drawRect(0, 350, 400, 25);
land.graphics.endFill();
addChild(land);

function keyPressed(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP :
attemptJump = true;
break;
case Keyboard.LEFT :
goleft = true;
break;
case Keyboard.RIGHT :
goright = true;
break;
}
}
function keyReleased(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP :
attemptJump = false;
break;
case Keyboard.LEFT :
goleft = false;
break;
case Keyboard.RIGHT :
goright = false;
break;
}
}

function run(event:Event) {
char.y+=velocity;
velocity+=gravity;
if (char.hitTestObject(land)) {
velocity = 0;
}
if (attemptJump==true) {
velocity =-5;
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}






Click inside the demo to focus than use the arrow keys to move and jump. You will notice a problem where you can float, we will fix this later, but for now the new code...explained!

Lines 5-6: New event listeners for the key up and key down events.
lines 9-10: These are new speed variables, left speed and right speed.
lines 11-13: These are boolean statements, true or false statements. They are declared now rather than when i use them.
Key Pressed Function: This function is called whenever a key is pressed. When the UP key is pressed the attemptjump boolean = true. When the LEFT and RIGHT keys are pressed the booleans goleft and goleft are called true respectively.
Key Released Function: This function is called whenever a key is released. In the event of the UP key being released attemptjump is called false, the other booleans are also called false.
Run function: I have made some changes to the run function, where i make use of the attemptJump, goleft and goright booleans. Another if statement for each boolean. eg if (attemptJump==true) . This is called whenever the attemptJump boolean is true, so when the UP key is pressed and it makes the velocity = -5. I use 2 more if statements for the other booleans, where i increase the "char"'s x position by lspeed, if goleft = true, or rspeed, if goright = true.

Now to sort out that unlimited jumping issue.

I think we can sort this by only allowing the player to jump if its touching the floor.

some code, just the new run function.

function run(event:Event) {
char.y+=velocity;
velocity+=gravity;
if (char.hitTestObject(land)) {
velocity = 0;
if (attemptJump==true) {
velocity =-5;
}
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}

All i have changed is put the if (attemptJump==true) statement inside the hittestobject statement. This means that it only checks to see if the player is attempting to jump if the "char" movieClip is touching the "land" movieClip.

A demo is in order!



Thats it for the 2nd part of my platform tutorials. Please wait for the next in the series.

No comments: