AS3 在班级之间调度自定义事件

发布于 2024-11-02 04:40:45 字数 2574 浏览 0 评论 0原文

我想从 Country() 调度一个自定义事件到 MenuButton();

国家/地区事件

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

}

当悬停在一个国家/地区时,会调度一个自定义事件,我希望菜单按钮侦听该事件,并且如果传递的参数与其名称相同,则突出显示。 Country 类是我在舞台上的国家电影剪辑的基类,MenuButton 是菜单按钮的基类

似乎该事件永远不会完成

提前感谢

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

}
Country Class

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 Class

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

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猛虎独行 2024-11-09 04:40:45

您必须进行两项修改:

首先,将事件 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); 
}

You have to make two modifications:

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

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); 
}
微暖i 2024-11-09 04:40:45

解决此问题的最佳且简单的方法是简单地将阶段引用作为类级别参数传递,并将事件添加到阶段引用并将事件分派到阶段引用。

国家/地区类

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

the best and easy way for solving this issue is by simply pass the stage reference as class level parameter and add event to the stage reference and dispatch event to the stage reference.

Country Class

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 Class

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文