What can I use instead of Timers to help my performance?

82 views Asked by At

My app is very slow on mobile devices.

It uses a lot of Event.ENTER_FRAME event listeners, so I switched (as I read they are much more performance heavy) to a global timer that these classes listen for if they need to update (they only use these timers if they have been initiated by a TouchEvent).

I also tried enabling hardware acceleration, and switching to using the CPU, but these have not helped with the lag issues to a noticable amount. (my app has very few animations so I think this is the best decision)

I do have a lot of MovieClips in it, however very few of them actually animate, so I doubt this is an issue. (This is also why I do not freeze and unfreeze objects, as they are never removed from the stage)

On this website they said the following about timers

– the more timer instances linger in the system, the more you can expect slow and choppy gameplay or unresponsive UI.

I only use one timer, so I didn't think this would be an issue, but I do have a lot of event listeners that use this timer to update, so I can only figure that the timer must be the issue. Thus to avoid lag I am looking for a better alternative.

To be clear, I'm not using many Event.ENTER_FRAME event listeners anymore, I've mostly switched to timers. With that being said, here is an example of a more resource demanding one.

package 
{

    import flash.display.MovieClip;
    import flash.events.Event;

    public class mc_BattleScene extends MovieClip
    {
        public static var mc_PlayerFace:MovieClip;
        public static var enemyAttackEffect:MovieClip;
        public static var mc_playerHitBox:MovieClip;

        public static var battler1:Number = 1;
        public static var battler2:Number = 1;
        public static var battler3:Number = 1;

        public var lvlModifier:Number;
        public var dmgReduction:Number;
        public var dmgDealt:Number;

        public static var dmgSpeed:Number = 1;

        public function mc_BattleScene()
        {
            visible = false;
            addEventListener(Event.ENTER_FRAME, onFrame);
            mc_PlayerFace = playerFace_mc;
            enemyAttackEffect = attackEffect_mc;
            mc_playerHitBox = playerHitBox_mc;

            function onFrame(e:Event):void
            {
                battler1_mc.gotoAndStop(battler1);
                battler2_mc.gotoAndStop(battler2);
                battler3_mc.gotoAndStop(battler3);

                if (Game.playerInteractionStatus[1])
                {
                    //we are fighting
                    visible = true;
                    if (attackEffect_mc.hitTestObject(playerHitBox_mc))
                    {
                        // attack hit us, deal dmg
                        dmgReduction = (Game.playerStats[2] * (Game.enemyStats[1]));
                        dmgDealt = Game.enemyStats[1] - dmgReduction;
                        attackEffect_mc.x = 516;
                        if (Game.playerStats[0] - Math.round(dmgDealt) <= 0)
                        {
                            // round dmg to 0 (were dead)
                            Game.playerStats[0] = 0;

                        }
                        else
                        {
                            // deal damage to us
                            Game.playerStats[0] -=  Math.round(dmgDealt);
                        }
                    }
                    else if(attackEffect_mc.hitTestObject(wall))
                    {
                        //stop the player from moving (by doing nothing)
                    }
                    else
                    {
                        attackEffect_mc.x -=  dmgSpeed;
                    }
                }
                else
                {
                    // reset the position of the attack effect if we are not fighting
                    visible = false;
                    attackEffect_mc.x = 516;
                }
            }
        }
    }
}

This example of Event.ENTER_FRAME is one of the few that actually are this complicated, the other Event.ENTER_FRAMEs tend to simply update values (ex: update a text field to display correct number variables)

For example: creating multiple vars inside an enterframe could clog the memory and cause lag.

I've avoided doing stuff like this, as not only what you've said, but in my opinion I think it doesn't make sense to continuously define it in a function, I update these values only.

Resources

http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-8000.html

max links allowed

0

There are 0 answers