【问题标题】:Flex 4 - Problem listening to an event in Main.mxml from a UIComponent extended custom classFlex 4 - 从 UIComponent 扩展自定义类监听 Main.mxml 中的事件时出现问题
【发布时间】: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


【解决方案1】:

问题解决了。实际上我在 dispatchEvent 时撒了一点谎,因为为了测试目的,我 dispatchEvent 附加构造函数初始化过程。好吧,我的错我对纯动作脚本更有经验和舒适 - 但我真的不能在组件的构造函数初始化过程完成时从组件监听应用程序 Main.mxml 中的事件?因为之后一切正常。

【讨论】:

    猜你喜欢
    • 2010-11-16
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2023-03-07
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多