2016-01-18 20 views
0

我想在閃光燈中做一個簡單的點亮插板。我已經完成了1個掛鉤的一般邏輯,但總共會有2,300個掛鉤,我不想爲每個影片剪輯添加一個事件監聽器。簡單的方法來使很多按鈕共享一個功能? AS3

這裏是我的代碼:

import flash.events.Event; 
var my_color:ColorTransform = new ColorTransform(); 
movieClip_1.addEventListener(MouseEvent.MOUSE_UP, fl_MouseClickHandler); 
function fl_MouseClickHandler(event:MouseEvent):void 
{ 

    if (my_color.color == 0) 
    { 
     my_color.color = 0x0000FF; 
     event.target.transform.colorTransform = my_color; 
    } 
    else if (my_color.color == 255) 
    { 
     my_color.color = 0x00FF00; 
     event.target.transform.colorTransform = my_color; 
    } 
    else if (my_color.color == 65280) 
    { 
     my_color.color = 0xFF0000; 
     event.target.transform.colorTransform = my_color; 
    } 
    else if (my_color.color == 16711680) 
    { 
     my_color.color = 0xFFFFFF; 
     event.target.transform.colorTransform = my_color; 
    } 
    else if (my_color.color == 16777215) 
    { 
     my_color.color = 0x000000; 
     event.target.transform.colorTransform = my_color; 
    } 
    else 
    { 
     trace(my_color.color); 
    } 
} 

[Screenshot of Final Product]

回答

2

這裏有3種方法來實現:

  1. 把代碼就掛自己的時間表。 (或創建一個類文件,並將其附加到您的掛鉤對象)。這將自動爲每個Peg實例重新使用相同的代碼。只要看看你有相同的代碼,但使用this關鍵字,而不是硬參考影片剪輯:

    var my_color:ColorTransform = new ColorTransform(); 
    this.addEventListener(MouseEvent.MOUSE_UP, fl_MouseClickHandler); 
    function fl_MouseClickHandler(event:MouseEvent):void 
    { 
        //..... 
    
  2. 做一個容器雪碧/影片剪輯,並已全部釘是它的唯一的孩子。然後遍歷該容器的所有兒童和附加監聽器:

    //loop through all children of the container and add an event listener 
    var i:int = container.numChildren; 
    
    while(i--){ 
        container.getChildAt(i).addEventListener(....); 
    } 
    

    這是一件好事,因爲你沒有給他們實例名,這將是相當繁瑣。

  3. 將點擊偵聽器附加到所有掛鉤的共同父級,並使用事件的目標屬性來查看點擊是否在掛鉤上。 假設你已經用鼠標右鍵點擊你的PEG庫對象,去屬性和檢查「爲ActionScript導出」,並給它的類名「MyPeg」,你可以這樣做:

    commonParent.addEventListener(MouseEvent.CLICK, parentClick); 
    
    function parentClick(e:Event):void { 
        if(e.target is MyPeg){ 
         //it's a PEG, do something 
        } 
    } 
    

    現在,這取決於如何你掛鉤對象是結構化的,目標也可以指代掛鉤的一個孩子(而不是掛釘本身)。爲了避免這種情況發生,如果適用,您可以禁用掛鉤子的鼠標輸入。所以,你盯住對象的第一幀上,你可以這樣:this.mouseChildren = false;


現在,甚至更好的(不那麼單調乏味)將通過代碼來實例釘了。所以如前所述,導出你的peg在它的屬性中的actionscript,並給它一個類的名字(「MyPeg」對於我的例子)。然後東西沿着這些線路:

var curRow:int = 0; 
var curCol:int = 0; 

var totalRows:int = 25; 
var totalCols:int = 92; 

var startingY:int = 10; 
var startingX:int = 10; 

var padding:int = 2; //gap between pegs 

var curPeg:MyPeg; 

while(true){ 
    //create the peg, and add it to the display. 
    curPeg = new MyPeg(); 
    addChild(curPeg); 

    //add the click listener to this peg 
    curPeg.addEventListener(MouseEvent.CLICK, fl_mouseClickHandler); 

    //assign the position of this peg 
    curPeg.x = startingX + (curCol * (curPeg.width + padding)); 
    curPeg.y = startingY + (curRow * (curPeg.height + padding)); 

    //increment the column 
    curCol++; 

    //check if we've reached the last column in the row 
    if(curCol >= totalCols - 1){ 
     //yes, so reset the column to 0 and increment the row 
     curCol = 0; 
     curRow++; 

     //break out of the loop if the curRow exceeds or is equal to the total rows var 
     if(curRow >= totalRows) break; 
    } 
} 

這種方式,你可以簡單地通過修改分配給totalColstotalRows數更改網格的大小 - 無需繁瑣走動2300對象的FlashPro。

+0

如果您爲每個掛鉤使用了一個Class文件,那麼必須有權訪問paren'ts顏色屬性(壞習慣),否則將不得不派遣父母仍需監聽的事件。儘管這可能是一個自定義事件,但這更容易。 –

+0

@AmyBlankenship - 我沒有看到父母如何參與peg的顏色? – BadFeelingAboutThis

+0

my_color是由父級擁有的ColorTransform。它看起來像是在每次點擊時切換一系列顏色,所以比每個掛鉤更高的東西需要跟蹤我們所在的顏色。 –

0

一種方法是循環遍歷你的2300個父節點的所有子節點。

 
for (var i:int=0; i<numChildren; i++) { 
    var clip = getChildAt(i); 
    if (clip.name.indexOf('movieClip_')==0) { 
    clip.addEventListener((MouseEvent.MOUSE_UP, fl_MouseClickHandler); 
    } 
} 

另一種方式來做到這一點是一個處理程序添加到整個父剪輯,然後在處理程序檢查,看看發生了什麼點擊的是釘之一。但是,您必須在子剪輯上禁用mouseChildren才能運行。

請注意,您可能希望查看用switch/case替換那個大的if/then語句,在這種情況下它更清晰且更緊湊。

相關問題