2011-04-20 74 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); 
    } 
}