开发者

Best way to optimize my homebrew Flex game?

I have a question about making flash games in Flex. Right now I am developing a 2D fighting game, here's the link: http://xoz.netai.net/stickfightjx/StickFightJX.html

However, I am not sure how efficient I programmed the main loop.

Since I have never read a tutorial or seen any other Adobe Flex games out there, I didn't know how to implement them开发者_如何学C. I have a Timer instance that calls an update function every 24 frames, which updates all of the game objects and views.

Is there any way to make this faster? Am I wasting anything?

// This is in an MXML Application

private var timer:Timer = new Timer(1 / 24);

private function onInitialize(e:Event):void
{
    timer.addEventListener(TimerEvent.TIMER, onUpdate);
    timer.start();
}

private function onUpdate(e:TimerEvent):void
{
    gameStateManager.update();
}


What you have should work fine, but you might run into trouble on slow computers where the frame rate is lower than you expect.

For smooth animations you'll want to listen for the ENTER_FRAME event instead. Your update() method should take the time delta since the last frame as a parameter. You can use this to base your calculations off time instead of off frames.

For example:

...
// Called from your original code.
private function update():void
{
    xPos += 10;
    yPos += 5;
}
...

In this case, the updating object will have its xPos and yPos members increased by 10 and 5 respectively as often as the TIMER event gets called. On slow computers, the CPU won't be able to keep up and the time between updates will be longer and slow down your entire game, making the fights much easier.

Updates will also be out of sync with the frame rate, causing animations to look choppy. This happens because sometimes there will be more than one update before the canvas is rendered again, meaning the object will change its xPos and yPos by 20 and 10 for that frame instead.

...
// Called from ENTER_FRAME with the time since the last call passed in.
private function update(deltaT:Number):void
{
    xPos += 10 * deltaT;
    yPos += 5 * deltaT;
}
...

In this case, the updating object will change its xPos and yPos members by 10 and 5 respectively once per second (if your deltaT is in seconds) regardless of how slow the CPU is. Updates also happen once and only once for every time the canvas is rendered since you're using the ENTER_FRAME event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜