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.
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;
}
}



August 11th, 2008 at 4:44 pm
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!
August 16th, 2008 at 1:22 pm
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”.
September 26th, 2008 at 3:00 pm
Would really love the next tutorial:)