0
\$\begingroup\$

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;

    }

}
}
\$\endgroup\$
2
  • \$\begingroup\$ I think it would be OK if you worded the title of the question a bit better, it's pretty vague. \$\endgroup\$
    – jcora
    Commented Jul 7, 2012 at 19:22
  • \$\begingroup\$ 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. \$\endgroup\$
    – bummzack
    Commented Jul 8, 2012 at 11:05

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This was a lesson I needed to learn the hard way. All things done easy may not be all things done right. \$\endgroup\$
    – joncodo
    Commented Dec 20, 2012 at 13:25
2
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Jul 7, 2012 at 18:40
  • \$\begingroup\$ No problem!! I just found out a few days ago myself! Cheers! \$\endgroup\$
    – Jan
    Commented Jul 7, 2012 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .