0

我在将两个脚本合二为一时遇到问题。

这是脚本的主要部分:AS3。

这已经是加入脚本了。

这是我需要导入的部分代码(AS2):

stop();
var banners:Array = new Array();
var imagePaths:Array = new Array();
var links:Array = new Array();
var bodyTexts:Array = new Array();
var imageTime:Number;
var numberOfBanners:Number;
var isRandom:String;
var showHeader:String;
var bannersXML:XML = new XML();
bannersXML.ignoreWhite = true;
bannersXML.load("banners.xml");
bannersXML.onLoad = function(success) {
    if (success) {
        trace("XML LOADED");
        imageTime = parseInt(this.firstChild.firstChild.firstChild)*1000;
        numberOfBanners = parseInt(this.firstChild.childNodes[1].firstChild);
        isRandom = this.firstChild.attributes["isRandom"];
        showHeader = this.firstChild.childNodes[2].attributes["showHeader"];

        var bannerSequence:Array = new Array();
        if (isRandom == "true") {
            //Make a random sequence
            while (bannerSequence.length<numberOfBanners) {
                newRandomNumber = random(numberOfBanners);
                //Make sure that the random one chosen is not already chosen
                for (var i = 0; i<=bannerSequence.length; i++) {
                    if (newRandomNumber != bannerSequence[i]) {
                        alreadyThere = false;
                    } else {
                        alreadyThere = true;
                        break;
                    }
                }
                //Add only random values that aren't in the array
                if (!alreadyThere) {
                    bannerSequence.push(newRandomNumber);
                }
            }
        } else {
            for (var i = 0; i<numberOfBanners; i++) {
                bannerSequence.push(i);
            }
        }
    }

    //Read XML in the Random Order Chosen
    for (var i = 0; i<numberOfBanners; i++) {
        banners.push(this.firstChild.childNodes[2].childNodes[bannerSequence[i]].firstChild.firstChild.toString());
        bodyTexts.push(this.firstChild.childNodes[2].childNodes[bannerSequence[i]].childNodes[1].firstChild.nodeValue);
        imagePaths.push(this.firstChild.childNodes[2].childNodes[bannerSequence[i]].childNodes[2].firstChild.nodeValue);
        links.push(this.firstChild.childNodes[2].childNodes[bannerSequence[i]].childNodes[3].firstChild.nodeValue);
    }
    play();
};
//Start the image counter at 0
var imageCounter = 0;

我在这部分代码中遇到错误

function doRandArray(a:Array):Array {//make random array
 var nLen:Number = a.length;
 var aRand:Array = a.slice();
 var nRand:Number;
 var oTemp:Object;
 for (var i:Number = 0; i < nLen; i++) {
  oTemp = aRand[i];
  nRand = i + (random(nLen – i));
  aRand[i] = aRand[nRand];
  aRand[nRand] = oTemp;
 }
 return aRand;
}

当我运行它时,我在这个地方得到一个错误:

nRand = i + (random(nLen – i));
Scene 1, Layer 'Layer 1', Frame 1, Line 265 1084: Syntax error: expecting rightparen before i.
4

2 回答 2

0

as2random(random(nLen – i));生成 0,1,...nLen-i-1。不只浮动 int 值。

正确的 as3 代码是int(Math.random()*(nLen-i));Math.floor(Math.random()*(nLen-i));

as2:随机()

as3: Math.random()

于 2013-03-04T23:24:29.353 回答
-1

ActionScript 3中,随机函数与在 as2 代码中的函数有点不同,只需将有问题的行更改为:

nRand = i + Math.random()*(nLen-1);

这应该可以修复所有错误并正常工作。

编辑:正如@bitmapdata.com 所指出的,为了使其与 as2 中的运行相同,必须截断随机值(去除其十进制值)。除了他建议的几种可能性之外,我个人只会在声明时将 nRand 的类型更改为 uint:

var nRand:uint;

您还可以将迭代器类型更改为var i:uint. 更少的内存使用总是好的;)

于 2013-03-04T17:28:51.690 回答