Josh Brittain
Josh Brittain

Reputation: 2212

gotoAndPlay not working for externally loaded SWF

I have an external swf that is a character animation. I successfully load it and store it in a MovieClip variable and add it to the stage. It starts playing at frame 0 and everything works fine except that I can't get gotoAndPlay or gotoAndStop to work. I can trace out the currentFrame of the MovieClip, so I have access to the timeline. I don't get any flash errors when using gotoAndPlay, or gotoAndStop it just simple isn't doing anything to the MovieClip.

I am at a loss as to why everything but this would work. Any tips greatly appreciated.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoaderComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, LoaderError);
loader.load(new URLRequest("file.swf");

private function LoaderComplete(e:Event):void 
{
    var character:MovieClip = e.target.content as MovieClip;
    stage.addChild(character);
    character.gotoAndStop(10);
}

Upvotes: 0

Views: 525

Answers (2)

Chicken
Chicken

Reputation: 43

It could be that the moment you adding it to the stage, it is unknown it has multiple frames. Try listening for the added_to_stage event and THEN goto the frame, like:

private function LoaderComplete(e:Event):void 
{
  var character:MovieClip = e.target.content as MovieClip;
  stage.addChild(character);
  character.addEventListener(Event.ADDED_TO_STAGE, gotoTheFrame);
}

private function gotoTheFrame(e:Event){
  var target:MovieClip = MovieClip(e.target);
  target.gotoAndStop(10);
} 

Upvotes: 1

Nicolas Siver
Nicolas Siver

Reputation: 2885

I think main timeline of the loaded swf file isn't your character, that is why you are getting always currentFrame 1.

//This line of code, gives you reference on main timeline
var mainTimeline:MovieClip = e.target.content as MovieClip; 
//Check if there is character
//If I'm correct, numChildren will return 1 or more
trace(mainTimeline.numChildren);
//If yes, access character animation
//Like this:
var char: MovieClip = mainTimeline.getChildAt(0) as MovieClip;
char.gotoAndStop(10);

Upvotes: 1

Related Questions