lundi 28 novembre 2016

AS3 gotoAndStop events trigger enterFrame but only after loading external files

OK, we've found a solution to this problem but we're not happy that we understand it and so I wondered if anyone can explain it for me - maybe it'll lead us to a slightly less hacky answer.

So. We've got a complex AS3 project that loads in various external files. What we've discovered is that once this has happened, gotoAndStop commands (not necessarily on the externally loaded files) trigger the EnterFrame event we have attached to the main timeline. This leads to some really weird results and occasionally complete crashes.

The example we found that showed us we had a problem looked something like this:

(Bit of code in one place)

trace("goto 1")
tClip.gotoAndStop("on")
trace("goto 1 done")

(bit of code somewhere else)

trace("goto 2")
tClip2.gotoAndStop("off")
trace(goto 2 done")

output:

goto 1
goto 2
goto 2 done
goto 1 done

Note that this behaviour only seems to happen if we've previously downloaded some external files. Otherwise we get

goto 1
goto 1 done
goto 2
goto 2 done

as we'd expect.

The workround we've come up with is to put all our gotoAndStop commands into a new function:

public function fGoToFrame(tClip:MovieClip, tFrame:*):void {
    pIgnoreFrameEventFromGoToAndStop = true
    tClip.gotoAndStop(tFrame)
    pIgnoreFrameEventFromGoToAndStop = false
}
// frame event:
public function fAction(e:Event):void {
    if (!pIgnoreFrameEventFromGoToAndStop) {
...

}
}

That seems to prevent any of the bugs, but it's not exactly satisfying. I'd like to know why gotoAndStop on a child movieClip would trigger a frame event on the root sprite at all! And is there any way of stopping it without using this hack?

Thanks for any thoughts

Aucun commentaire:

Enregistrer un commentaire