RCIX
RCIX

Reputation: 39427

Haxe -- code refuses to work

I have the following code:

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();
 }
}

but the only trace output i get is "Game started"; nothing else is tracing or working. Why?

Update: After having fixed the screenBounds problem, the following problems remain:

Update: since the first problem is a separate issue, i'm splitting that out into another question.

Upvotes: 0

Views: 299

Answers (3)

Justinfront
Justinfront

Reputation: 472

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;
}

Upvotes: 0

Andy Li
Andy Li

Reputation: 6034

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.

Upvotes: 6

Preet Sangha
Preet Sangha

Reputation: 65466

Since

    trace("Game constructor");

is not printing I'd say the error is in one of the lines above it.

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

comment them out and introduce them one at a time till "Game constructor" is not longer displayed. Then you know where to start investigating.

Upvotes: 1

Related Questions