简体   繁体   中英

AS3 random .swf loading

im making a site that need to have random videos load on the front page. i have couple of swfs on the root, and i made a .swf named random that will load on the main page (uploaded in same folder with the swfs) and hopefully it will load one of the movies at a time. No luck so far. this is the code i use

    stop(); 
var movieArray:Array = ['1', '2', '3'];
var loader:Loader = new Loader(); 
var index:int = movieArray.length * Math.random(); 
var url:String = movieArray[index] + '.swf'; 
trace("Attempting to load", url); 
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete); 
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError); 
addChild(loader); 
function loaderComplete(e:Event):void {     
    trace("Successfully loaded", url);
    } function loaderIOError(e:IOErrorEvent):void {     
    trace("Failed to load", url); 
    }

oh, i use AS3. and the vids are made in AS3 too. Any ideas? thnx.

I think that this particular line is probably causing your issue:

var index:int = movieArray.length * Math.random();

Specifically, there is no guarantee that movieArray.length * Math.random() will yield an int type. You need to wrap the operation inside of Math.floor() to ensure you get an int that's within the bounds of your array:

var index:int = Math.floor(movieArray.length * Math.random());

If you're still not seeing your swf movies load then there is likely an issue elsewhere also, but my solution is the simplest to start with and if it works you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM