Skip to main content
Athaariq
Participating Frequently
August 14, 2016
Answered

Force Stop "For" Loop when Frame Updated

  • August 14, 2016
  • 2 replies
  • 637 views

Hi all,

I've put a "for" loop into "Enter Frame" event function, but it causes flash became not realtime. Is there any way to stop the loop when the stage render a new frame?

the code below as example:

addEventListener(Event.ENTER_FRAME, update)

function update(e:Event) {

     for(var i:int = 0; i<300000; i++) {

          // My actions here

         

          // Stop command here when frame updated

     }

}

I'm using Adobe Flash Pro CS6 with ActionScript 3. Please for your help..

Thank you

This topic has been closed for replies.
Correct answer ClayUUID

What you want is impossible. ActionScript is not multithreaded, so it has no way in the middle of executing code to "know" when it's time to render the next frame.  Event handler code runs to completion, always. It's the responsibility of the programmer to ensure that event handlers don't run for too long.

The only solution to your problem is to do less processing each frame. For example determine how many pixels can be processed before slowdown occurs (bearing in mind that this limit will be different for different users), then limit your loop to processing that many pixels at a time, perhaps iteratively processing the entire bitmap, or randomly selecting pixels instead. If you wanted to get really fancy you could include a benchmarking algorithm to automatically determine what the upper limit is on any particular system before processing time exceeds the frame rate.

Also your randomHex() function can be completely replaced with Math.random()*16777215. That should speed things up a bit. Saving a local reference to bmp.bitmapData.width and height in the update function instead of looking it up every single time through the loop should also be faster.

2 replies

Brainiac
August 14, 2016

What on Earth are you needing to do 300000 times every frame?

Athaariq
AthaariqAuthor
Participating Frequently
August 15, 2016

Clay’s Unique UID wrote:

What on Earth are you needing to do 300000 times every frame?

That's just a simple example to make you more easier to understand

ClayUUIDCorrect answer
Brainiac
August 15, 2016

What you want is impossible. ActionScript is not multithreaded, so it has no way in the middle of executing code to "know" when it's time to render the next frame.  Event handler code runs to completion, always. It's the responsibility of the programmer to ensure that event handlers don't run for too long.

The only solution to your problem is to do less processing each frame. For example determine how many pixels can be processed before slowdown occurs (bearing in mind that this limit will be different for different users), then limit your loop to processing that many pixels at a time, perhaps iteratively processing the entire bitmap, or randomly selecting pixels instead. If you wanted to get really fancy you could include a benchmarking algorithm to automatically determine what the upper limit is on any particular system before processing time exceeds the frame rate.

Also your randomHex() function can be completely replaced with Math.random()*16777215. That should speed things up a bit. Saving a local reference to bmp.bitmapData.width and height in the update function instead of looking it up every single time through the loop should also be faster.

robdillon
Participating Frequently
August 14, 2016

An Enterframe event fires every time the playback head moves through a frame, so the simplest way to fix your problem might be to use a different event to execute your for loop. Maybe if you describe in more detail what you want to achieve, someone can give you a more effective method to do that.

Athaariq
AthaariqAuthor
Participating Frequently
August 15, 2016

the code above just a simple example. I'm trying to create my own realtime noise bitmap effect. When the frame need to be updated real-timely, the loop must be stopped and the bitmap should be pushed to display even it isn't complete. This is the real code:

--------------------------------------------------------------------------------------------------------------------------------

import flash.display.Bitmap;

import flash.display.BitmapData;

import flash.events.Event;

var bmp:Bitmap = new Bitmap(new BitmapData(1080, 720))

bmp.alpha = 0.5

addChild(bmp)

addEventListener(Event.ENTER_FRAME, update)

function update(e:Event) {

     for(var iy:int = 0; iy<bmp.bitmapData.height; iy++) {

          for(var ix:int = 0; ix<bmp.bitmapData.width; ix++) {

               bmp.bitmapData.setPixel(ix, iy, randomHex())

               // add stop loop command here when frame updated

          }

     }

}

function randomHex():uint {

     var r:int = Math.random()*255

     var g:int = Math.random()*255

     var b:int = Math.random()*255

     return ( (r<<16) | (g<<8) | b )

}

--------------------------------------------------------------------------------------------------------------------------------