【发布时间】:2010-09-17 17:35:20
【问题描述】:
我遇到了奇怪的问题。我有一个扩展 UIComponent 的主游戏类,并将所有游戏逻辑基本粘合在一起 - 主循环。然后我有一个初始化主游戏类的应用程序 main.mxml 文件,请注意游戏屏幕状态(主菜单、游戏、游戏结束等)并添加一些 ui 控件 - flex 非常适合。尽管如此,当我尝试在 Main.mxml 中侦听在 Game 类中调度的自定义事件时出现问题.
**GameStateEvent.as**
public class GameStateEvent extends Event
{
public static const GAME_OVER:String = "gameOver";
public static const NEW_LEVEL:String = "newLevel";
public function GameStateEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
override public function clone():Event
{
return new GameStateEvent(type, bubbles, cancelable);
}
}
Game.as
[Event(name="gameOver", type="custom.events.GameStateEvent")]
public class Game extends UIComponent
private function checkforEndGame():void
{
if(_gameOver == true)
{
dispatchEvent(new GameStateEvent(GameStateEvent.GAME_OVER)); //true
}
}
Main.mxml
<fx:Script>
<![CDATA[
protected function game_gameOverHandler(event:GameStateEvent):void
{
//This event never got dispatched
}
]]>
</fx:Script>
<game:Game id="game" includeIn="GameState" gameOver="game_gameOverHandler(event)"/>
我真的对此很感兴趣 - 事情看起来很简单,但由于我未知的原因,似乎没有任何工作。我尝试捕获,冒泡事件 - 没有,事件从未在 Main.mxml 中调度。
【问题讨论】:
-
一切对我来说都是正确的。从事件监听器备份。你确定事件被触发了吗?
-
我使用 Flextras,这一切看起来都不错,假设 dispatchEvent 真的在 checkForEndGame 中被调用。我能想到的唯一一件事:GameStateEvent 真的在你的 custom.events 包中吗?
-
是的,事件被触发是因为 dispatchEvent 返回 true 并且如果我在 Game.as 中添加EventListener,当我在 Main.mxml 中 dispatchEvent 但由于未知原因在 Game.as 中 dispatchEvent 并收听时,侦听器得到了相同的响应在 Main.mxml 中的监听器从未被击中。
标签: apache-flex actionscript-3 events flex4