AS3从一个类到另一个类调度自定义事件

时间:2011-04-20 09:35:51

标签: actionscript-3 events dispatch

我想从Country()sto MenuButton()发送自定义事件;

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
        {

        }
    }

}

MenuButton类

package  {

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


    public class MenuButton extends MovieClip {

        public var countryName:String = "";

        public function MenuButton() {

            this.buttonMode = true;
            this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
            this.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);
        }
    }

}

当一个国家/地区悬停时,会调度一个自定义事件,我希望MenuButton监听,并且传递的参数与其名称相同以突出显示。 Country Country是我在舞台上使用的movieclips的基类,MenuButton是菜单按钮的Base Class

似乎事件永远不会通过

提前致谢

2 个答案:

答案 0 :(得分: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); 
}

答案 1 :(得分: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
    {

    }
}

MenuButton类

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);
    }
}