Loading external pngs into an AS2 swf that is load

2019-07-21 00:11发布

问题:

I have a Wrapper SWF that loads a series of AS2 movies. Each AS2 movie loads a series of .png files.

AS3_wrapper.swf
 |-> AS2_1.swf
      |-> image_1.png
      |-> image_2.png
 |-> AS2_2.swf
      |-> image_1.png
      |-> image_2.png

Inside of the AS2 I listen for the load of the pngs using onLoadInit and update my UI.

This works fine for the first AS2 swf. But when I load the second AS2 swf the onLoadInit isn't triggered for the pngs. My guess is that the images are in a cache or something like that. I put a random string on the end of the request to try and avoid the cache but that doesn't seem to work.

The code in the as2 looks roughly like this:

var flagLoader:MovieClipLoader = new MovieClipLoader();
var listener:Object = new Object();
listener.onLoadInit = Delegate.create(this, handleImageLoad);
flagLoader.addListener(listener);
var row:MovieClip = frame1["row" + (numLoaded + 1)];
flagLoader.loadClip(predictionData[numLoaded].flag + "?r="+Math.random(), row.flag);

I'm making sure to load only one image at a time (I've read anecdotal evidence loading more than one thing at a time can confuse the MovieClipLoader). For the first as2 file everything works great. When I load the second as2 file the handleImageLoad never gets called.

Update: Even more perplexing is if I reload the first AS2 movie (after the second AS2 movie fails to load the images) the first AS2 movie loads the images again fine.

Update 2: After trying to change from using a MovieClipLoader to polling (as was helpfully suggested) I have found some more evidence that is relevant.

When I load the first AS2 files and trace from the top level clip it prints out _root. The second AS2 file when loaded traces the same _root. This lead me to check if they were clashing on names and they are. Both have a child called frame. The first one, when I trace it comes out as _root.frame as expected. The second AS2 file traces _level0.instance3.instance118.instance111.frame. I'm guessing this is related to the problem.

Flash is keeping the _root of the two files the same but it is changing the locations of their children (for subsequently loaded files that have children with the same names). So either the onLoad is going to the wrong clip or the events about it loading are.

回答1:

Once every 3-4 years I come across a problem that I think will finally break me...

I came across this post when trying to figure out why even though I was successfully loading my AVM1 movie into an AVM2 movie file, the AVM1 file was not running as expected.

Having solved the initial huge issue of not being able to communicate between AS2 - AS3 files I couldn't believe it when the AVM1 movie always seemed to halt shortly after starting playback.

For me, the MovieClipLoader loadClip() methods were not firing any progress or completed events which was consequently causing my file to stop playback completely, as you both suggested.

Your suggestion to run an interval after the loadClip call to compare bytes loaded to bytes total worked for me.

Thank you Thank you Thank you Thank you Thank you ! :D



回答2:

Running AS2 under AS3 seem to introduce some latency issues.... you need to poll the loaded swf a few times before it recognizes it's loaded.

I'm in a similar situation ... an AS3 Shell loading a large and complicated AS2 application with many external swfs, dependencies, etc. Wat I had to do was to run an interval after the loadClip call to compare bytes loaded to bytes total... then when they equaled each other (but exceeded 4) I could safely fire the onLoadInit function that would otherwise never fire.

So you lose all the init, progress and error events that MovieClipLoader gave you in AS2.. you're going back to FP5 and FP6 days but it does seem to work reliably. Would love to hear about other peoples experiences and if there is a better way around this.



回答3:

James,

You're right, I've also noticed that _root vs _level0.instance3.instance118 read-out in the trace stack you you described, it must be related to the AVM1 content and how AVM2 movies handle it.

Polling won't always work but you can use it to see what's happening after you fire the loadClip() method. Do you see your bytesLoaded as undefined? Or maybe the swf is loaded but you can't access properties of it, such as a variable declared on the main timeline of your swf. I've gotten pretty deep into this problem and found that essentially you have two or three ways of working around these problems. Polling the AS2 container is the first step to see what is failing....

Can you get your .pngs to load in using loadMovie instead of MovieClipLoader?

Have you tried running your example under FP10.1? Oddly enough a lot of these issues go away!

Depending on the complexity of your AS2 application this can be a tough problem to work around.

You checked this out: http://fladdict.net/blog-en/2007/06/avm2loader-class.html

thanks, Geoff