-2

我从未使用过 AS2,我想知道您如何将其转换为 AS3?任何帮助将不胜感激。

stop();
var nr:Number = 0;
var $mc:MovieClip;

this.onEnterFrame = function (){
    if(nr < 100)
    {
            $mc = this.attachMovie("fire", "fire"+nr, nr);
            $mc._x = random(4)-2;
            $mc._yscale = 80;
            $mc._rotation = random(2)-1;
            random(2) == 0 ? $mc._xscale = 80:$mc._xscale = -80; 
            nr++;
    } else {
        nr = 0;
    }
}
4

1 回答 1

0

请参阅下面重构代码中的内联注释:

stop();
var ctr:int = 0;  //var to keep track of how many instances you've made of the Fire class 
var mc:DisplayObject;  //temporary object to hold the created instance of the FIre class

this.addEventListener(Event.ENTER_FRAME,onEnterFrame); //this is how you do enter frame handlers in AS3

function onEnterFrame(e:Event):void {
    if(ctr < 100)
    {
            /*
               instead of doing the attach movie clip, you have give your Fire object it's own class / actionscript linkage, then you instantiate it and add it to the display list with addChild()
            */    

            mc = new FireClass();
            mc.x = random(4)-2;   //in AS3 there is just Math.Random(), which returns a number between 0 and 1.  I've made a random() function below that emulates the old random() function.  Also, ._x & ._y are now just .x & .y
            mc.scaleY = .8;  //scaleX/Y have changed names, and 0 - 100 is now 0 - 1.
            mc.rotation = random(2)-1;
            mc.scaleX = random(2) == 0 ? .8 : -.8; 
            addChild(mc);
            ctr++;
    } else {
        this.removeEventListener(Event.ENTER_FRAME,onEnterFrame); //remove the listener since ctr has reached the max and there's no point in having this function running every frame still
    }
}

function random(seed:int = 1):Number {
    return Math.Random() * seed;
    //if you're expecting a whole number, you'll want to to this instead:
    return Math.round(Math.random() * seed);  //if seed is 4, this will return 0,1,2,3 or 4
}
于 2013-03-25T22:08:34.087 回答