Tuesday, September 25, 2007

ENTER_FRAME problem

i've got an interesting conundrum for you all.

According to Flash help, ENTER_FRAME "...does not go through a 'capture phase' and is dispatched directly to the target, whether the target is on the display list or not."

and it is true that an object with an ENTER_FRAME event does seem to work okay if it's not on the display list.

however, if you start loading external assets, it seems to forget about this ENTER_FRAME event at some random point in time! insert the code below into a frame, and replace imageFile with an actual external asset. the enterFrame function should count up from 1 to infinity, but instead at around 30(for me) it stops!

however, if you add the MovieClip to the stage, it receives the ENTER_FRAME event without fail.

const imageFile:String="put a swf here.swf";
var tally:int=0;
var loadertally:int=0;
setUpEnterFrame();
nextLoader();
function setUpEnterFrame() {
var newObject:MovieClip=new MovieClip();
newObject.addEventListener(Event.ENTER_FRAME,enterFrame);
//this.addChild(newObject); -------works when these comments are removed...
}
function nextLoader() {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderDisplayed);
var request:URLRequest = new URLRequest(imageFile);
loader.load(request);
moveToRandomPlace(loader);
this.addChild(loader);
}
function enterFrame(event:Event):void {
tally++;
trace(">>>>enterFrame - "+tally);
}
function loaderDisplayed(event:Event):void {
loadertally++;
if (loadertally<200)>
nextLoader();
}
trace("loader complete - "+loadertally);
}
function moveToRandomPlace(whichObject:DisplayObject):void {
whichObject.x=(Math.random()*stage.stageWidth);
whichObject.y=(Math.random()*stage.stageHeight);
}

STOP PRESS!

I've resolved this issue with some credit to kglad in forums - the problem is that as newObject is a variable local to the nextLoader function, when this function is complete and is garbage collected, the EnterFrame no longer triggers... i initially encountered this problem with Tweens - it is a very easy mistake to make - something to be cautious of!

No comments: