views:

1398

answers:

4

Hopefully this won't be taken as asking the same question twice...

So I'm working on a Flash website (in AS2) which has an outer index swf which loads sub swf files using loadMovie("subfoo1.swf", placeToShowSwf). These in turn load an xml file which tells it what content to load. Everything works peachy, but we'd like to add a button to the index swf that opens a sub swf file with one or two different values for one or two variables.

Unfortunately, just adding a button that says

loadMovie("foo1.swf", placeToShowSwf);
placeToShowSwf.openProject(x);

doesn't work, I assume because openProject(x) is called on a file that isn't fully loaded. I know that there's not a problem with the code, because I made a button elsewhere that only calls placeToShowSwf.openProject(x) and there aren't any problems.

I see two solutions, both of which I'm unsure how to do.

  1. Change the desired value when the swf file is made, like a constructor for a class. But is there some sort of constructor function for swf files? It'd be really nice just to say loadMovie(new foo1.swf(x), placeToShowSwf) or something equivalent.
  2. Wait until after swf (and probably xml) is loaded, and then call placeToShowSwf.openProject(x).

Anyone got any guidance towards either of these solutions, or perhaps some other way that my pea-like brain has been unable to fathom?

A: 

Number 2 is what you should do. Unfortunately you are still stuck in ActionScript 1 land, while ActionScript 3 has been out for a couple years now. So I don't know the exact syntax (having started with flash on ActionScript 2 myself), but #2 (waiting for SWF to finish loading, then telling it what to do) is what I would do.

If you don't mind learning a little bit of AS2 (you can mix AS1 and AS2 without too much problem usually), then you should look at MovieClipLoader. It provides callbacks to tell you when a sub SWF has finished loading.

davr
A: 

Three easy ways to approach this:

(1) - reverse the direction of the command. That is, in the parent SWF you have:

projectType = "x";
loadMovie("foo1.swf", placeToShowSwf);

And the child (loaded) SWF has this:

openProject( _parent.projectType );

You get the idea. This won't work if the child SWF is loaded from a different server, unless you address the cross-domain scripting problems.

(2) - wait for the MC to load. Unfortunately AS2 doesn't have built in load events that you can use, so a common method is to use an onEnterFrame like this:

someMovieClip.onEnterFrame = function() {
    if ( path_to.placeToShowSwf.openProject ) {
        path_to.placeToShowSwf.openProject(x);
        this.onEnterFrame = null;
    }
}

That would be if you want to call the method as soon as it's available; if you want to wait until the child swf is fully loaded you'd want to check the child SWF's getBytesLoaded() and getBytesTotal() methods.

(3) Use a component like MovieClipLoader to do your loading - just follow the docs. Under the surface this is similar to (2), but the details are tucked away inside the MovieClipLoader class.

fenomas
A: 

fenomas answer is pretty decent, but there also a less 'hacky' way if you're using AS2 load your swfs using the MovieClipLoader class. It has events that you can listen for and you will be interested in the onLoadInit Event. Although people might intuit that it get's triggered when the loading process is initialized, and onLoadComplete should be the thing to use, it's not the case.

onLoadComplete gets triggered when all the bytes finished downloading onLoadInit gets triggered when all the bytes finished downloading and the classes in the loaded swf are initialized ( so you can access the loaded swf's methods and properties )

Have a look at the code sample in MovieClipLoader > onLoadInit in the as2.0 documentation

http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Part4_ASLR2.html

Good luck

George Profenza
A: 

The way you have done is the right one except that you need to wait for the onLoadInit callback before trying to access your sub-MovieClip.

Theo.T