Building a Flash Platformer
- by Jonathan O
I am basically making a game where the whole game is run in the onEnterFrame method. This is causing a delay in my code that makes debugging and testing difficult. Should programming an entire platformer in this method be efficient enough for me to run hundreds of lines of code?
Also, do variables in flash get updated immediately? Are there just lots of threads listening at the same time?
Here is the code...
stage.addEventListener(Event.ENTER_FRAME, onEnter);
function onEnter(e:Event):void
{
//Jumping
if (Yoshi.y > groundBaseLevel)
{
dy = 0;
canJump = true;
onGround = true; //This line is not updated in time
}
if (Key.isDown(Key.UP) && canJump)
{
dy = -10;
canJump = false;
onGround = false; //This line is not updated in time
}
if(!onGround)
{
dy += gravity;
Yoshi.y += dy;
}
//limit screen boundaries
//character movement
if (! Yoshi.hitTestObject(Platform)) //no collision detected
{
if (Key.isDown(Key.RIGHT))
{
speed += 4;
speed *= friction;
Yoshi.x = Yoshi.x + movementIncrement + speed;
Yoshi.scaleX = 1;
Yoshi.gotoAndStop('Walking');
}
else if (Key.isDown(Key.LEFT))
{
speed -= 4;
speed *= friction;
Yoshi.x = Yoshi.x - movementIncrement + speed;
Yoshi.scaleX = -1;
Yoshi.gotoAndStop('Walking');
}
else
{
speed *= friction;
Yoshi.x = Yoshi.x + speed;
Yoshi.gotoAndStop('Still');
}
}
else //bounce back collision detected
{
if(Yoshi.hitTestPoint(Platform.x - Platform.width/2, Platform.y - Platform.height/2, false))
{
trace('collision left');
Yoshi.x -=20;
}
if(Yoshi.hitTestPoint(Platform.x, Platform.y - Platform.height/2, false))
{
trace('collision top');
onGround=true; //This update is not happening in time
speed = 0;
}
}
}