2011-06-04 64 views
0

我會在要求清晰的部分放置一個「#」號! 這是從這個鏈接的代碼 http://www.emanueleferonato.com/2008/05/02/creation-of-a-matching-game-with-flash-and-as3/關於在actionscript中添加屬性的問題3

我知道這個鏈接的詳細解釋,但我還是不代碼 哈哈

包{

import flash.display.Sprite; 
import flash.events.MouseEvent; 
import flash.events.TimerEvent; 
import flash.utils.Timer; 

    public class color_match extends Sprite { 

    private var first_tile:colors; 
    private var second_tile:colors; 
    private var pause_timer:Timer; 
    var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8); 
    public function color_match() { 
     for (x=1; x<=4; x++) { 
      for (y=1; y<=4; y++) { 
     var random_card = Math.floor(Math.random()*colordeck.length); 
       var tile:colors = new colors(); 
            //why is .col attribute not declared? 
            //# 
        tile.col = colordeck[random_card]; 
       colordeck.splice(random_card,1); 
       tile.gotoAndStop(9); 
       tile.x = (x-1)*82; 
       tile.y = (y-1)*82; 
       tile.addEventListener(MouseEvent.CLICK,tile_clicked); 
       addChild(tile); 
      } 
     } 
    } 
    public function tile_clicked(event:MouseEvent) { 
        //what does "as colors" suppose to mean here, can i just omit it?, 
        //does the type of any display obj with event.currentTarget/target 
        //generates a type OBJECT         
        //# 
     var clicked:colors = (event.currentTarget as colors); 
     if (first_tile == null) { 
      first_tile = clicked; 
      first_tile.gotoAndStop(clicked.col); 
     } 
     else if (second_tile == null && first_tile != clicked) { 
      second_tile = clicked; 
      second_tile.gotoAndStop(clicked.col); 
      if (first_tile.col == second_tile.col) { 
       pause_timer = new Timer(1000,1); 
       pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles); 
       pause_timer.start(); 
      } 
      else { 
       pause_timer = new Timer(1000,1); 
       pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles); 
       pause_timer.start(); 
      } 
     } 
    } 
    public function reset_tiles(event:TimerEvent) { 
     first_tile.gotoAndStop(9); 
     second_tile.gotoAndStop(9); 
     first_tile = null; 
     second_tile = null; 
     pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles); 
    } 
    public function remove_tiles(event:TimerEvent) { 
     removeChild(first_tile); 
     removeChild(second_tile); 
     first_tile = null; 
     second_tile = null; 
     pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles); 
    } 
} 

}

的某些部分
+0

在這個代碼中的方式 var tile = new colors(); 顏色是一個盒子的動畫片段與九個關鍵幀的鏈接,每個關鍵幀表示一個顏色 – christian 2011-06-04 03:47:58

回答

0

第一個問題,「爲什麼.col屬性沒有聲明?」是因爲該對象是一個動態類。這意味着您可以在對象上設置任何名稱的屬性,以便稍後可以檢索它們。在這種情況下,它被用來存儲稍後在tile_clicked()函數中使用的值。這可以稱爲任何東西,例如tile.myvalue = colordeck [random_card];值得注意的是,使用像這樣的動態屬性是靈活的,但是你將失去任何代碼暗示你的IDE可能會給。

第二個問題,「顏色是什麼意思?」是一種類型演員。 event.current target只是一個通用對象,只要flash知道並試圖將一個通用對象分配給一個聲明爲特定類型的變量可能會導致編譯器錯誤。通過轉換對象(告訴編譯器,您知道該對象應該是什麼),可以消除這些錯誤。

+0

這個動態屬性如.col可以被該類別的一個實例訪問嗎? 並且它可以被任何代碼訪問? – christian 2011-06-04 04:22:40

+0

該屬性不會被任何東西自動訪問,但它是一個公共屬性,因此任何具有對該對象的引用的代碼都將能夠訪問該屬性。 爲了更好地理解,請嘗試使用Object,基類的一切東西。 'var o1:Object = new Object(); o1.foo =「test」; var o2:Object = new Object(); o2.bar =「test 2」; trace(o1.foo); // test trace(o2.foo); //這將導致空引用錯誤 trace(o2.bar); // test 2 trace(o1.bar); //這將導致空引用錯誤' 使用動態屬性,它們只存在於單個實例上。 – HotN 2011-06-04 17:30:11