gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:41:14 27 4
gpt4 key购买 nike

我想将自定义事件从 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);
}
}

}

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

事件好像一直没过完

提前致谢

最佳答案

你必须做两个修改:

首先,将您的事件 bubbles 属性设置为 true,这样当 Country 剪辑分派(dispatch)一个事件时,它将上升到顶层.

那么您的 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);
}

关于actionscript-3 - AS3 从一个类到另一个类调度自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5728258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com