2013-03-25 47 views
-2

我從來沒有使用AS2,我想知道如何將其轉換爲AS3?任何幫助將不勝感激。將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; 
    } 
} 
+2

在未來,這將是最好的證明你已經嘗試過,然後集中你的問題的組成部分:你不能自己弄清楚。 – BadFeelingAboutThis 2013-03-25 22:57:35

回答

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 
}