Skip to main content
Known Participant
May 29, 2014
Question

Loading Lag - How to Alleviate?

  • May 29, 2014
  • 2 replies
  • 1803 views

My game runs smoothly, but when a bitmap is first loaded onto the screen (usually more than one at a time) it causes some lag. The lag doesn't reoccur until you exit the app and restart it. I am curious as how to preload these images before they appear on the screen so I can make it look smoother. Its all in one SWF (no external files), and the whole SWF is only 10 MB (probably under 20MB when completed).

How do I preload the images so they don't cause lag during game play? (Like load it on a transitioning/loading screen)

This topic has been closed for replies.

2 replies

Known Participant
June 1, 2014

What kind of performance can I expect out of a mobile device? I feel like I'm not performing that many or complex operations and I still get disproportionate amounts of lag. Just switching from Math.pow(x, 3) to x*x*x was a drastic performance increase, and any kind of graphics seem to strain the system so much regardless if they are vector or bitmaps.

What kind of operations put lots of stress on the GPU/CPU (like Math.pow)?

Does alpha/transparency of Bitmaps create that much distress for the GPU?

What are common hidden culprits that suck the time of the GPU/CPU?

How can I tell if its a memory allocation problem vs processing problem?

(Also on an unrelated note, using Vector.<T> breaks the code formatter in Flash CC. It will change "Vector.<T>" into "Vector. < T > " and then the compiler complains!)

Colin Holgate
Inspiring
June 1, 2014

If you’re using GPU mode, then alpha shouldn’t affect the performance, having bitmaps that are too big will. So will having lots of bitmaps that are too big. If you set the stage quality to medium or high, it will increase the CPU requirements for getting the bitmap onto the GPU.

Something else that will make a big difference is if you are using cacheasbitmap on a movieclip that is changing. You could choose not to use cacheasbitmap at all, and trust Flash to get your bitmaps and vectors over to the GPU. That’s what I do these days.

So long as you’re not doing anything that forces textures to have to reload to the GPU, it shouldn’t make any performance difference whether they were vectors or bitmaps in the first place.

Known Participant
June 1, 2014

I tried cacheAsBitmap and it made things worse so Im not using it.

Is object reuse super important? Specifically, does object reuse matter if I am already removing instances I'm not using? (ie I remove children and make it null inside the code) Is making a new object really too much for mobile?

And if anyone knows about the Vector.<T> formatting bug any help fixing it would be nice!

Participating Frequently
May 29, 2014

Do you mind explaining what do you mean by loading your bitmap? Are you loading your bitmap with code, or are you just putting them on the timeline? It would help a lot if there are more info.

If you are doing it in timeline, I'm guessing this is what's happening.

1. bitmap exist somewhere in frame 1, but not inside the scene.

2. bitmap doesn't exist on frame 1, it exists further down the timeline.

Maybe you can try putting the bitmap on frame 1 inside the scene, then hide it (invisible=false, move outside of scene, hide behind another object, etc) until you use it.

Known Participant
May 29, 2014

Yes, they are being put onto the stage via code and are "Exported on Frame 1" but it still causes lag the first time it actually shows up in view. This effect only happens on iOS, not he SWF or AIR Mobile.

Projectitis
Inspiring
May 29, 2014

That is a common problem, and can occur for (at least) two reasons:

  • It takes time to assign memory for the bitmap/data as it is created
  • It takes time to transfer the bitmap data to the GPU.

My technique is to have an array of BitmapData ('cache') and keep a reference to each BitmapData in there.  When I create a new Bitmap I grab the BitmapData from the cache.  Multiple bitmaps will share the same BitmapData instance. This means that all the memory is assigned before the game starts and the slowdown doesn't happen in the game.  Something like this:

// This adds all your BitmapDatas into a cache so that they all exist in memory at the outset of the game

private var bdCache : Vector.<BitmapData> = new Vector.<BitmapData>([

     new MyBitmapData1(),

     new MyOtherBitmapData(),

     new MySpriteBitmapData(),

     ...

     new MyLastBitmapData() // These are all your different bitmap data's

]);

// To create a Bitmap at any time from the cache

var mySprite : Bitmap = new Bitmap( bdCache( 2 ) ); // Create Bitmap using MySpriteBitmapData


If you still get lag when a Bitmap is created, it will be reason 2 (takes time to transfer to GPU).  To solve this, you can place an instance of each bitmap on the stage (off the screen) before the game starts to force the bitmap into the GPU.  You may be able to remove them again on the next frame, or perhaps not (sometimes if you remove them from the stage, they are also removed from GPU - mostly depends on how much texture memory you are using on the GPU - i.e. how big your Bitmaps are).


Hope it helps,

Peter