• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Force Stop "For" Loop when Frame Updated

Community Beginner ,
Aug 14, 2016 Aug 14, 2016

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

408

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 15, 2016 Aug 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 dif

...

Votes

Translate

Translate
LEGEND ,
Aug 14, 2016 Aug 14, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 15, 2016 Aug 15, 2016

Copy link to clipboard

Copied

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 )

}

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 14, 2016 Aug 14, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 15, 2016 Aug 15, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 15, 2016 Aug 15, 2016

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines