AS3 Preloader Cache Problem?
So the following code is Lee's implementation of a preloader which works fine first load but goes crazy when the browser loads a cached file, jumping from 0% to 100% randomly
Things I have tried to no avail:
- switching off gzip compression, tho I dont think it is on
Using ENTER_FRAME instead of progress and complete
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop); l.contentLoaderInfo.addEventListener(Event.COMPLETE, done); l.load(new URLRequest("http://www.foo.com/foo.swf")); function loop(e:ProgressEvent):void { perc = Math.round(e.bytesLoaded / e.bytesTotal * 100); lt.text = String(perc); if (perc >= 100) l.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loop); } function done(e:Event):void { l.contentLoaderInfo.removeEventListener(Event.COMPLETE, done); addChild(l); }
I do not believe I am loading it more than once:
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
Security.allowDoma开发者_Go百科in("http://www.foo.com");
preLoader();
}
I discovered the problem happens because the files are hosted on an addon domain.
To fix this I changed the url to reference the main domain:
from
l.load(new URLRequest("http://www.foo.com/foo.swf"))
to
l.load(new URLRequest("http://foo.maindomain.com/foo.swf"))
and also add
Security.allowDomain("http://foo.maindomain.com");
精彩评论