Flex 4 - 从UIComponent扩展自定义类中侦听Main.mxml中的事件时出现问题

时间:2010-09-17 17:35:20

标签: flex actionscript-3 events flex4

我遇到了奇怪的问题。我有一个主要的游戏类,它扩展了UIComponent并将所有游戏逻辑粘合在一起 - 主循环。然后我使用app 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中发送。

1 个答案:

答案 0 :(得分:0)

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

相关问题