views:

17

answers:

1

I'm trying to use addFrameScript() on a SWF animation I have loaded but am running into a few problems. Here's what works right now:

public function project() {
    var loader:Loader = new Loader();
    loader.load(new URLRequest("animation.swf"));
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, animLoadComplete, false, 0, true);
}

private function animLoadComplete(e:Event):void {
    anim = e.target.content as MovieClip;
    addChild(anim);
    //anim.addFrameScript(anim.totalFrames - 1, animEnd);
}

private function animEnd():void {
    trace("animEnd");
}

Like this, the animation plays fin and just loops over and over again.

The trouble seems to be that the animation runs regardless of using stop(), play() or any animation function. A trace(anim.totalFrames) also shows that my animation is 2 frames rather than 23 (which it is).

When I un-comment anim.addFrameScript(anim.totalFrames - 1, animEnd); the frame script appears to be called every frame and the animation ceases to play and is instead replaced by the flash "loading dots" where it should appear.

I suppose my question is twofold. Am I loading in my animation properly and why does a framescript cause my animation to disappear?

A: 

Hi there, pathing / referencing for that stop() can be tricky when using this stuff.

I built some utilities to make it easier because I was doing this a lot - the utils deal with preserving existing code too: http://flair-flash-flex-air.blogspot.com/2010/07/using-actionscript-3-to-inject-code.html

There's a link to a gist of the util classes I created within the article.

But the fact that your loaded swf is tracing as having 2 frames rather than the 23 you expect is - as you noticed - worrying! I expect this is causing your visible problem (the code tracing 'every frame') as if you've only got 2 frames then your script would be running on alternate frames.

Have you tried listening for INIT instead of COMPLETE? I usually find this more reliable if I want to use the swf's contents. Also you might (depending on your parent swf) need to set the SecurityDomain of the LoaderContext property (you'd pass this in when you do your load() - check out the AS3 docs for Loader for more details).

Stray