2012-12-29 125 views
0

我接手了一個項目,我真的是新來的as3!我需要解決一個未解決的問題,希望不必從頭開始。是否可以解決這個問題?

我有一個contaner_mc,舉辦了四個puppet_mc的(puppet_mc1puppet_mc2puppet_mc3puppet_mc4),每個puppet_mc擁有四個按鈕(buddy_bnt_1buddy_bnt_2buddy_bnt_3buddy_bnt_4)。

我需要聽衆知道點擊了哪個按鈕以及點擊了哪個按鈕?

Object(this).contaner_mc.puppet_mc?.buddy_bnt_?.addEventListener(MouseEvent.CLICK, fl_Click); 

function fl_Click(event:MouseEvent):void 
{ 
    gotoAndPlay(15); 
} 

任何意見將不勝感激!

回答

1

事件冒泡應魔:

container_mc.addEventListener(MouseEvent.CLICK, click_handler); 

function click_handler(event:MouseEvent):void { 
    if (event.currentTarget == event.target) { 
    return; // the container is the dispatcher 
    } 

    const sprite:Sprite = event.target as Sprite; // just for a typing 
    if (!sprite) { 
    trace("event.target is no sprite, set break point here for debugging."); 
    return; 
    } 

    if (sprite.name == 'buddy_bnt_1' || sprite.name == 'puppet_mc1') { 
    // do something 
    } else if (sprite.name == 'buddy_bnt_2' || sprite.name == 'puppet_mc2') { 
    // do something different 
    } else if (sprite.name == 'buddy_bnt_3' || sprite.name == 'puppet_mc3') { 
    // do something different 
    } else if (sprite.name == 'buddy_bnt_4' || sprite.name == 'puppet_mc4') { 
    // do something different 
    } 
} 
+0

嘿謝謝你的快速回復!但我得到這個錯誤類型錯誤:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t at chatV3_fla :: MainTimeline/click_handler() – shelfish

+0

看起來轉換爲精靈已損壞。你使用圖像作爲按鈕嗎? –

+0

是的..那是不好的? – shelfish

0
container_mc.addEventListener(MouseEvent.CLICK, clickHandler); 

function clickHandler(event:MouseEvent):void 
{ 
    var clickedObject:DisplayObject = event.target as DisplayObject; 

    trace (clickedObject.name); // name of button clicked 
    trace (clickedObject.parent.name); // name of the parent 
} 

我用的DisplayObject,這樣的代碼將與精靈,影片剪輯,或任何的DisplayObject工作。所以請確保您進口,如果你還沒有:

import flash.display.DisplayObject; 
+0

「clickedObject.name」省長謝謝大家! – shelfish

+0

接受答案。 – prototypical

相關問題