Friday 28 March 2008

creating a platform game in as3 part 3

You should read this and this before continuing, if you haven't already. 

This little platform engine works fine at the moment, but most platform games have more than one platform. i will add another platform and see how that goes.



click inside the demo to gain focus and then use the arrows to move.

if you move right on the screen you will see that we have a problem. the character is floating on the screen!! This is because we are using hitTestObject and not hitTestPoint. Lets change that.

So we can make some more complex terrain, i have made a new movieClip linkaged as ground, and another one linkaged as character. These replace the land and char movieclips that we drew in actionscript earlier.

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 character();
char.x=200;
char.y=0;
stage.addChild(char);

var land:MovieClip = new ground();
land.x=200;
land.y=300;
stage.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) {
velocity+=gravity;
if (land.hitTestPoint(char.x, char.y+char.height/2, true)) {
velocity = 0;
char.y--;
if (attemptJump==true) {
velocity =-5;
}

} else {
char.y += velocity;
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}

The most important thing that i have changed is the hitTest function - if (land.hitTestPoint(char.x, char.y+char.height/2, true))  This is a much better way of doing it than hitTestObject because you can have angled platforms. Lets explain the if statement. if the "char" movieClip's foot hits the land than do the function that follows. Lets focus on the char.y+char.height/2 this declares the "foot" of the moviclip as the registration point of "char" is in its centre. so centre + half the height gives the bottom.

The next thing that you will notice i have changed is to put a char.y-- in the hitTest function. this just keeps the "char" movieclip on the top of the floor, so it doesn't sink through it.

The only other thing that i have changed is to add an "else" statement to the if statement that checks the hitTest between the "char" movieclip and the "ground" movieclip, and move the line of code that makes the "char" fall (char.y += velocity;) into it. This just means that it only makes the "char" fall if its not touching the ground movieclip.

Demo




As you can see from the demo, you can successfully walk up and down slopes without problem. But the character seems to vibrate, which is kind of weird in a platform game! This is easily rectified and i will show you how in the next tutorial.

2 comments:

Unknown said...

Thanks for the tutorial - this is great!

I'm having a problem that I guess is related to something I'm doing wrong since no one else has commented on this. When I get to this part of the tutorial, my game no longer works (I see nothing), and I get two errors:

Frame 1, Line 15: 1180: Call to a possibly undefined method character.
Code: var char:MovieClip = new character();

Frame 1, Line 20: 1180: Call to a possibly undefined method ground.
Code: var land:MovieClip = new ground();

I'm very new to ActionScript, so maybe there's something very basic that I'm not doing. I know the code is pasted in my document just as you have it on your site - I've tried it several times.

Any help would be greatly appreciated. Thanks!

tudway said...

Sorry i haven't got back to you sooner (i was on holiday!) your getting these errors because you need to create a movieclip with a linkage of "character"and one with a linkage of "ground".