1

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

Tim Kenny
  • 11
  • 2
  • If you "switched to GPU" by selecting it in drop-down somewhere in the Publish Settings, then you should know it won't help. Unless you switch to GPU-enabled framework (like Gamua Starling), Flash will still use CPU to render graphics, and it is **indeed** slow on mobile devices. Script performance is not that much of an issue unless you perform heavy calculations each frame. – Organis May 31 '17 at 00:06
  • usually there's no need to use more than one enter_frame handler, also avoid vector graphics as much as possible and use bitmaps instead. as Organis said, use Starling for GPU acceleration. – Patang May 31 '17 at 09:31
  • 1
    Can you [edit the Question](https://stackoverflow.com/posts/44273670/edit) to include an example of code inside an `Event.ENTER_FRAME` function? For example: creating multiple vars inside an enterframe could clog the memory and cause lag. For such cases just make vars outside of any functions and only update their values within enterframe. Also use `someMC.cacheAsBitmap` for MovieClips that do not animate, or just use a static image (jpg, png) etc etc... Hard to advise on invisible issues. Share some enterframe code we can analyse... – VC.One May 31 '17 at 12:32
  • This is too broad to answer currently. You need to show some code to get real help with your performance issues. – BadFeelingAboutThis May 31 '17 at 15:47
  • > Also use someMC.cacheAsBitmap for MovieClips that do not animate, A question about this, most of my MovieClips have a bitmap, and a movieclip inside of them (ex: chair and an invisible box outlining a Hitbox) if I cached as a bitmap, would they no longer run their respective classes? as bitmaps can not have their own classes, but movieclips can – Tim Kenny May 31 '17 at 19:56
  • @BadFeelingAboutThis i dont know what's causing the performance issues, thus i do not know what code to provide, as i have a lot of classes, I have updated it to provide an example of a 'Event.ENTER_FRAME' function I have. Hopefully it is helpful – Tim Kenny May 31 '17 at 20:01
  • CacheAsBitmap just caches that display object as rasterized pixels, so you get a performance boost if you used that setting on a complex vector or a item that has lots of children/grandchildren that don't change. It will not change how any of your code/classes work. It's different than FlashPro's export as Bitmap setting which actually compiles your content as a bitmap. – BadFeelingAboutThis May 31 '17 at 21:23
  • Have you tried any profiling with tools like Adobe Scout? It can be a good tool for hunting down bottlenecks/performance issues. – BadFeelingAboutThis May 31 '17 at 21:24
  • Oh that's perfect, I will implement the CacheAsBitmap and see how the performance improves, also I have not used any profiling tools before, but I will try it out and report back – Tim Kenny May 31 '17 at 21:30
  • If you have CreativeCloud (or can do a free trial), definitely install Scout and run your swf through it. That will narrow down where your issue(s) are and give people a starting point on how to help. – BadFeelingAboutThis May 31 '17 at 21:31
  • @TimKenny .cacheAsBitmap is good for desktop computers, and not so good for mobiles. As soon as Flash will have to redraw things on screen, the performance will deteriorate, the larger the redraw area is - the worse, because Flash will still use CPU rendering, that is. I don't mean to discourage you, but GPU-enabled frameworks do not exist for nothing. I don't know how many objects you can have onstage with CPU rendering (without lags), but I bet it is not **that** many. Try it, of course. – Organis Jun 01 '17 at 11:47

0 Answers0