Welcome back, At the end of the last tutorial i wondered what would happen if we had a platform that you could jump underneath from.
In the end of the last post we had a problem where the character vibrated. This was because when ever the character touched the floor i made it move up by one pixel. to rectify this i will make another hittest that detects when the character is one pixel off the floor and only calculate the velocity of the player if it returns false.
velocity+=gravity;
if (land.hitTestPoint(char.x, char.y+char.height/2, true)) {
velocity = 0;
char.y–
}
if (!land.hitTestPoint(char.x, char.y+char.height/2+1, true)) {
char.y += velocity;
} else {
velocity=0;
if (attemptJump==true) {
char.y–;
velocity =-5;
}
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}
function run(event:Event) {
velocity+=gravity;
if (land.hitTestPoint(char.x, char.y+char.height/2, true)) {
velocity = 0;
char.y–;
}
if (!land.hitTestPoint(char.x, char.y+char.height/2+1, true)) {
char.y += velocity;
} else {
velocity=0;
if (attemptJump==true) {
char.y–;
velocity =-5;
}
}
if (!land.hitTestPoint(char.x-char.width/2, char.y+char.height/4, true)) {
if (goleft==true) {
char.x+=lspeed;
}
}
if (!land.hitTestPoint(char.x+char.width/2, char.y+char.height/4, true)) {
if (goright==true) {
char.x+=rspeed;
}
}
}
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;
}
}


