作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将自定义事件从 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/
我是一名优秀的程序员,十分优秀!