2011-04-20 142 views
3

我想從國家()STO的菜單按鈕()派遣一個自定義事件;AS3調度自定義事件從類

CountryEvent

package { 
import flash.events.Event; 

public class CountryEvent extends Event { 

    public static const COUNTRY_HOVERED:String = "onCountryOver"; 

    private var _countryName:String = ""; 

    public function CountryEvent(type:String, countryName:String, bubbles:Boolean=true, cancelable:Boolean=false) { 
     super(type, bubbles, cancelable); 
     _countryName = countryName; 
    } 

    public function get countryName():String { 
     return _countryName; 
    } 

    public override function clone():Event 
    { 
     return new CountryEvent(type,countryName,bubbles,cancelable); 
    } 
} 

} 國家類

package 
{ 

    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 
    import flash.events.Event; 

    public class Country extends MovieClip 
    { 
     private var countryEvent:CountryEvent; 


     public function Country() 
     { 
      this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver); 
      this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut); 
     } 

     private function onMouseOver(e:MouseEvent):void 
     { 

       countryEvent = new CountryEvent("onCountryOver",this.name); 

       dispatchEvent(countryEvent); 

      } 
     } 

     private function onMouseOut(e:MouseEvent):void 
     { 

     } 
    } 

} 

菜單按鈕類

​​

當一個國家徘徊在自定義事件進行調度,這是我想要的菜單按鈕來聽,如果傳遞的參數是一樣得到突出了它的名字。鄉村類的基類爲我的國家的影片剪輯我在舞臺上的菜單按鈕的菜單按鈕的基類

看來,事件從來沒有經歷過

感謝得到提前

+0

嗨,你的不同元素(國家,菜單按鈕),添加階段?他們有同一個家長嗎?您可能還希望,因爲他們是獨立的,你有問題,從你的榜樣刪除補間,並降低你的問題的可讀性。 – Kodiak 2011-04-20 09:59:04

+0

他們不是通過代碼添加,但他們是世界上影片剪輯的一部分。 world.Germany,world.Spain等。我將清除無關內容中的代碼。 – chchrist 2011-04-20 10:04:57

回答

5

你有進行兩項修改:

首先,將您的活動bubbles屬性設置爲true,因此當Country剪輯發送一個事件時,它將進入頂層。

那麼你MenuButtons應該聽stage,而不是自己。所以,當一個Country調度一個事件就上升到stage,並且可以通過按鈕來捕獲。如果你要聽的階段,你必須做你的代碼略有變化:

public function MenuButton() { 

    this.buttonMode = true; 
    this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver); 
    this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut); 
    this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
} 

private function onAddedToStage(e:Event):void 
{ 
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
    stage.addEventListener(CountryEvent.COUNTRY_HOVERED,onCountryOver); 
} 
+0

Thnx男人你搖滾!一個問題,爲什麼我必須等待added_to_stage事件? – chchrist 2011-04-20 10:26:45

+0

因爲如果你不這樣做,'stage'還不存在。 – Kodiak 2011-04-20 10:27:33

+0

我明白了...所以我想讓舞臺初始化。 Thanx幫助清除了我腦海中的一些東西 – chchrist 2011-04-20 10:36:04

0

解決這一問題是由簡單的傳遞階段引用作爲一流水平的參數,並添加事件最好的和簡單的方法階段參考和派發事件到階段參考。

國家類

package{ 

import flash.display.MovieClip; 
import flash.events.MouseEvent; 
import flash.events.Event; 

public class Country extends MovieClip 
{ 
    private var countryEvent:CountryEvent; 
    private var _stageRef:Stage; 


    public function Country(pStageRef:Stage) 
    { 
     _stageRef = pStageRef; 

     this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver); 
     this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut); 
    } 

    private function onMouseOver(e:MouseEvent):void 
    { 

      countryEvent = new CountryEvent("onCountryOver",this.name); 

      _stageRef.dispatchEvent(countryEvent); 

     } 
    } 

    private function onMouseOut(e:MouseEvent):void 
    { 

    } 
} 

菜單按鈕類

package { 

import flash.display.MovieClip; 
import flash.events.MouseEvent; 
import CountryEvent; 


public class MenuButton extends MovieClip { 

    public var countryName:String = ""; 
    private var _stageRef:Stage; 

    public function MenuButton(pStageRef:Stage) { 
     _stageRef = pStageRef; 
     this.buttonMode = true; 
     this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver); 
     this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut); 
     _stageRef.addEventListener(CountryEvent.COUNTRY_HOVERED,onCountryOver); 
    } 

    private function onCountryOver(e:CountryEvent):void { 
     if(e.countryName == countryName) { 
      this.gotoAndPlay(2); 
     } 
    } 

    private function onMouseOver(e:MouseEvent):void { 
     this.gotoAndPlay(2); 

    } 

    private function onMouseOut(e:MouseEvent):void { 
     this.gotoAndPlay(11); 
    } 
}