AS3调度自定义事件班班 [英] AS3 Dispatch Custom Event from class to class

查看:104
本文介绍了AS3调度自定义事件班班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从国家()STO的菜单按钮()派遣一个自定义事件;

I want to dispatch a custom event from the Country() sto the 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
        {

        }
    }

}

菜单按钮类

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

}

当一个国家盘旋,我想了菜单按钮来听,如果传递的参数是一样得到突出显示其名称自定义事件被调度。乡村类的基类为我的国家的影片剪辑我在舞台上的菜单按钮的基类菜单按钮

When a country is hovered a custom event is dispatched which I want the MenuButton to listen and if the parameter passed is the same as its name to get highlighted. The Country Class is the Base Class for my countries movieclips I have on stage and the MenuButton the Base Class for the menu button

看来,该事件从未得到过

It seems that the event never gets through

在此先感谢

推荐答案

您必须做出两处修改:

首先,将你的活动气泡属性,所以当国家片段发送一个事件就会上升到最高级别。

First, set your event bubbles property to true, so when a Country clip dispatches an event it will go up to the top level.

那么你的 MenuButtons 要听,而不是自己。因此,当一个国家调度一个事件就上升到,可以捕获按钮。如果你想要听的阶段,你必须做一个细微的变化在code:

Then your MenuButtons should listen to stage, not to themselves. So when a Country dispatches an event it goes up to stage and can be caught by the buttons. If you want to listen to the stage you have to do a slight change in your code:

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

这篇关于AS3调度自定义事件班班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆