0

我有以下代码:

package ;

import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.Lib;
import flash.utils.Timer;

/**
 * ...
 * @author RCIX
 */

class Main 
{
 static function main()
 {
  trace("game started");
  var game : Game = new Game();
  game.Run();
  trace("game finished");
 }
}
class Game extends DisplayObject
{
 var rectX : Int;
 var rectY : Int;
 var velocityX : Int;
 var velocityY : Int;

 var screenBounds : Rectangle;
 var graphics : Graphics;

 public function new()
 {
  super();
  screenBounds = Lib.current.getBounds(new DisplayObject());
  graphics = Lib.current.graphics;
  Lib.current.addChild(this);
  trace("Game constructor");
 }

 public function Run()
 {
  trace("Run");
  Lib.current.addEventListener(Event.ENTER_FRAME, OnFrameEnter);
  velocityX = 1;
  velocityY = 1;
 }
 function OnFrameEnter(event : Event)
 {
  trace("OnFrameEnter");
  graphics.beginFill(0xFFFFFF, 0xFFFFFF);
  graphics.drawRect(0, 0, screenBounds.width, screenBounds.height);
  graphics.endFill();
  Update();
 }
 function Update()
 {
  trace("Updating");
  if (rectX + 50 > screenBounds.width || 
      rectX < 0)
  {
   velocityX *= -1;
  }
  if (rectY + 50 > screenBounds.height || 
      rectY < 0)
  {
   velocityY *= -1;
  }
  rectX += 1;
  rectY += 1;
  graphics.beginFill(0xFF0000);
  graphics.drawRect(rectX, rectY, 50, 50);
  graphics.endFill();
 }
}

但我得到的唯一跟踪输出是“游戏开始”;没有其他东西在追踪或工作。为什么?

更新:修复screenBounds问题后,仍然存在以下问题:

  • 我的 OnFrameEnter 或 Update 调用都没有跟踪;为什么?
  • 从 DisplayObject 扩展我的 Game 类会使事情陷入停顿,并且永远不会进入我的任何其他代码,无论我是否调用super();构造函数。

更新:由于第一个问题是一个单独的问题,我将其拆分为另一个问题。

4

3 回答 3

6

From AS3 language reference:

DisplayObject is an abstract base class; therefore, you cannot call DisplayObject directly. Invoking new DisplayObject() throws an ArgumentError exception.

So basically you should extends Shape or Sprite instead of DisplayObject.

于 2010-09-03T04:40:54.330 回答
1

自从

    trace("Game constructor");

没有打印我会说错误在它上面的一行中。

    super();
    screenBounds = Lib.current.getBounds(new DisplayObject());
    graphics = Lib.current.graphics;
    Lib.current.addChild(this);

将它们注释掉并一次介绍它们,直到不再显示“游戏构造函数”。然后你知道从哪里开始调查。

于 2010-09-02T22:34:59.663 回答
0

As Andy says don't use DisplayObject. I suggest you use Sprite. DisplayObject is for example the return type of parent, which can be tricky, DisplayObject should only be used when dealing generically with Sprite, Shape, MovieClip etc... it should never be used for creating a display item, so only for use like an interface when you want to deal more generically with several types of display object.

For instance...

var sp: DisplayObject = cast new Sprite();
var mc: DisplayObject = cast new MovieClip();

setX( sp, 20 );
setX( mc, 20 );

function setX( do: DisplayObject, val: Float )
{
do.x += val;
}
于 2012-10-01T05:39:42.577 回答