Skip to main content
February 18, 2009
Answered

Duplicating Loaded SWF, No Actionscript??

  • February 18, 2009
  • 2 replies
  • 514 views
So... in writing a Flash based game, I have the main SWF and its associated classes at one point loading a single SWF that represents an animated sprite in the game. I want to use this sprite multiple times in the game, so I duplicate it after loading, and expected that this would work. Unfortunately, after duplicating the loaded SWF, it seems to be ignoring timeline actionscript calls such as gotoAndPlay().

Here's the setup:

The loaded SWF that contains the animated sprite has a single layer on the timeline, and basically has a number of labeled frames dedicated to each animation -- i.e., there is a "walk_east" frame, which cycles through a character walking east, and at the end of that animation, has a gotoAndPlay("walk_east") command on one of the frames that sends it back to the beginning of that particular animation to repeat the whole ordeal. The frame after that one might be "walk_west", and contain the walking west animation, etc. The very first frame of this SWF has a simple "stop()" call in it, so nothing animates until the main SWF tells this loaded SWF to do so (via a gotoAndPlay() call). This SWF has a base document class called "Actor".

The main SWF loads this SWF using a typical Loader. Then, to duplicate the loaded SWF, I settled on doing something like this, where "clip" is a reference to the loaded SWF loader:

var swfClass:Class = clip.contentLoaderInfo.applicationDomain.getDefinition("Actor");
var clone = new swfClass();

I can create all the duplicates I want, no problem. However, as soon as I start playing one (i.e., "clone.gotoAndPlay("walk_east"), it will just cycle through the entire timeline of the loaded / duplicated sprite SWF -- ignoring the various actionscript code on frames that SHOULD have it repeating the specific animation cycle I want... and despite the fact that when played stand-alone, the same SWF does not display this behavior.

Its as though duplicating the movieclip causes the duplicated SWF to lose its actionscript on the timeline?

Does anyone have any ideas? Is this expected behavior? I can't imagine this is how it should be -- I only want to load the SWF sprite one time, to save bandwidth, and then duplicate it as many times as I want in the game. This has had to be done before... I can't imagine developers simply reloading the same SWF multiple times if they need the same SWF to be used multiple times -- i.e., multiple enemies of the same sort.

Or am I just making this more difficult than it has to be? Please help!
This topic has been closed for replies.
Correct answer
RE: "The reason I use the Loader and don't just add the SWF to the main FLA's library is because the sprites to be loaded are dynamic based upon a number of variables, and dictated through an XML configuration file. The app only knows which SWFs to load at runtime based upon things like what level the player entered...."

Ok, so? You shouldn't need a loader for sprites just because they are constructed dynamically.
You can keep all the config in the XML files, but keep the logic in separate classes that are all referenced in the same FLA file. You say "...sprites to be loaded are dynamic based upon a number of variables..." Ok, can't you construct those sprites from parts that are in your library dynamically using the "new" operator? Try a different approach, don't get stuck on the technical problem that is most probably a bug in the VM..... If it doesn't work, just leave it. Try a different approach.

2 replies

February 18, 2009
Interesting test I just performed... I added the loader.content (the loaded movieclip) itself to the stage, as well as a clone of it. I called each's gotoAndPlay("walk_east").

The loader.content -- the movieclip SWF loaded via the Loader, plays perfectly -- the sprite animates and loops back when it hits the frame with the gotoAndPlay("walk_east") call.

The duplicated clone exhibits the "actionscript-less" behavior. Instead of going back to the appropriate frame when the frame with gotoAndPlay("walk_east") is hit, it simply moves into the next frame after that, and starts playing another animation it should not be -- i.e., it ignores the timeline actionscript.

So clearly, it's something to do with the duplicating code -- that's giving me some form of a copy, but it's losing it's timeline actionscript in the process!
February 18, 2009
Ok, I think your problem is not with cloning, but with making sure the playhead stops on the right frame. I would give the frames some labels
and then using the EVENT.ENTER_FRAME look throught each frame label and make sure you're on the correct frame....

In genereal I would avoid loading a component like Actor using the loader. Just put the actor movie inside your main fla file and just use
var actor:Actor = new Actor();

You can use the loader to load SWFs but keep in mind that you have to deal with whole bunch of security/ApplicationDomain tipe of s**t.
Why complicate your game? use the loader to load really heavy game components like a set of pictures or sounds... The rest can stay in your fla. Modular design doesn't mean you have to break up your application in to several swfs... instead try to keep the movieClips together and break your CODE (classes) properly. Sometimes you have to use the loader (like I said before) but use it sparingly only when you realy need to use it.
February 18, 2009
The reason I use the Loader and don't just add the SWF to the main FLA's library is because the sprites to be loaded are dynamic based upon a number of variables, and dictated through an XML configuration file. The app only knows which SWFs to load at runtime based upon things like what level the player entered, various flags and circumstances, etc.

I'm fairly certain it's not a mistaken frame situation, because playing the sprite SWF alone, without loading it into the main SWF, it plays back as expected -- when it hits a certain frame with a gotoAndPlay() call sending it back to loop a particular animation, it does exactly that... but when loaded into the main SWF with a Loader and then duplicated, all actionscript seems to be thrown out the door, oddly enough.
Correct answer
February 18, 2009
RE: "The reason I use the Loader and don't just add the SWF to the main FLA's library is because the sprites to be loaded are dynamic based upon a number of variables, and dictated through an XML configuration file. The app only knows which SWFs to load at runtime based upon things like what level the player entered...."

Ok, so? You shouldn't need a loader for sprites just because they are constructed dynamically.
You can keep all the config in the XML files, but keep the logic in separate classes that are all referenced in the same FLA file. You say "...sprites to be loaded are dynamic based upon a number of variables..." Ok, can't you construct those sprites from parts that are in your library dynamically using the "new" operator? Try a different approach, don't get stuck on the technical problem that is most probably a bug in the VM..... If it doesn't work, just leave it. Try a different approach.