Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;

    }

}
}
share|improve this question
    
I think it would be OK if you worded the title of the question a bit better, it's pretty vague. –  jco Jul 7 '12 at 19:22
    
FYI: onEnterFrame doesn't delay code. It does get executed at the start of every frame, as the name suggests. There's also an EXIT_FRAME event you could listen to... Debugging this is a simple as setting a break-point too. –  bummzack Jul 8 '12 at 11:05
add comment

2 Answers 2

up vote 3 down vote accepted

I don't think that writing the entire game logic in one event in a single class is the best way. It's better to divide the game logic in several classes and follow the best practices of OOP. Like having some base classes, like Level, Player, Enemy, Weapon etc. that separate game logic, collision detection, input handling, event handling etc. on a more logical way.

For Flash, there's another engine for making games: FlashPunk. It is free, has a big community and good tutorials to get started. Here's one tutorial for platformers that can be useful to you.

The problem is that there are differences between the current and older versions of FlashPunk so watch out for dates and versions on the forums and blogs when you see code samples.

share|improve this answer
1  
This was a lesson I needed to learn the hard way. All things done easy may not be all things done right. –  Jonathan O Dec 20 '12 at 13:25
add comment

If you're making a platformer, maybe you can try with the Citrus Engine. http://citrusengine.com It's a game engine specifically made for platform games and it open source

Cheers!

share|improve this answer
    
I tought Citrus engine was a commecial engine. Now I see it's open source for some time. Thanks for the info about Citrus being open source! –  Mentoliptus Jul 7 '12 at 18:40
    
No problem!! I just found out a few days ago myself! Cheers! –  Jan Jul 7 '12 at 18:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.