简体   繁体   English

AS3随机.swf加载

[英]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. 我在根上有几个swfs,我制作了一个名为random的.swf,它将在主页上加载(与swfs放在同一文件夹中),希望它一次可以加载其中一部电影。 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. 哦,我使用AS3。 and the vids are made in AS3 too. 西元也是在AS3中制作的。 Any ideas? 有任何想法吗? thnx. 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. 具体来说,不能保证movieArray.length * Math.random()会产生int类型。 You need to wrap the operation inside of Math.floor() to ensure you get an int that's within the bounds of your array: 您需要将操作包装在Math.floor()内部,以确保获得在数组范围内的int:

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. 如果您仍然看不到swf电影的加载,那么其他地方也可能存在问题,但是我的解决方案最简单,可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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