将AS2加入AS3时出错

时间:2013-03-04 17:15:07

标签: actionscript-3 actionscript-2

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

This is main part of the script: AS3.

And this is already joined script.

以下是我需要导入的代码的一部分(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.

2 个答案:

答案 0 :(得分:0)

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

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

as2:random()

as3:Math.random()

答案 1 :(得分:-1)

ActionScript 3 中,random功能与as2代码略有不同,只需将违规行更改为:

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

这应该可以解决所有错误并且工作方式相同。

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

var nRand:uint;

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