0

我是新手,需要一些帮助。

我正在用 Flash 制作游戏,但一直收到错误 1009。

我列出了我的游戏结束条件,每次发生游戏结束并调用 gotoAndPlay(1) 代码时,我都会收到错误消息。如果我注释掉 gotoAndPlay(1) 行,错误就会消失。

这是代码:

this.addEventListener(Event.ENTER_FRAME, gameOver);
    function gameOver(e:Event):void{
        //gameover conditions
        if (ball.y > 799 - ball.width / 2 && score > 0)
        {
            trace("GameOver!");
            if (this.contains(ball)){
            this.removeChild(ball);
            trace("Chicken removed");
            }
gotoAndPlay(1);
}
}

在此先感谢您的帮助!

4

2 回答 2

0

这可能是因为内联函数在它自己的(全局)范围内运行。它仍然可以访问函数中定义的变量,所以通常这样解决。

  this.addEventListener(Event.ENTER_FRAME, gameOver);
  var obj:DisplayObject = this;

  function gameOver(e:Event):void {
      //gameover conditions
      if (ball.y > 799 - ball.width / 2 && score > 0) {
          trace("GameOver!");
          if (this.contains(ball)) {
              this.removeChild(ball);
              trace("Chicken removed");
          }
          obj.gotoAndPlay(1);
      }
  }
于 2013-04-17T19:55:10.390 回答
0

更改了您的代码:

    this.addEventListener(Event.ENTER_FRAME, gameOver);
    function gameOver(e:Event):void{
        //gameover conditions
        if (ball.y > 799 - ball.width / 2 && score > 0)
        {
            trace("GameOver!");
            if (this.contains(ball)){
removeEventListener(Event.ENTER_FRAME, gameOver);
            this.removeChild(ball);
            trace("Chicken removed");
            }
gotoAndPlay(1);
}
}

添加了 removeEventListener。

于 2013-04-20T12:48:05.060 回答