0

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:

  • None of my OnFrameEnter or Update calls ever trace; why?
  • Extending my Game class from DisplayObject makes things grind to a halt and never get to any of my other code, regardless of whether i call super(); in the constructor.

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

Gama11
  • 31,714
  • 9
  • 78
  • 100
RCIX
  • 38,647
  • 50
  • 150
  • 207
  • I suggest, you install a debug player: http://www.adobe.com/support/flashplayer/downloads.html . it performs worse than the release player, but you get error messages and stack traces (for testing from FlashDevelop (which you seem to use), standalone debugger should suffice). Also, unlike in C#, in haXe by convention PascalCase is reserved to types and enum constructors – back2dos Sep 03 '10 at 13:40
  • Whoops, somehow i skipped right over the debug projector >. – RCIX Sep 03 '10 at 22:12
  • @back2dos: oik, it still doesn't work after getting the debug player. See http://stackoverflow.com/questions/3623363/flashdevelop-haxe-repeated-an-i-o-error-has-occured-errors for my problem – RCIX Sep 03 '10 at 22:25

3 Answers3

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.

Andy Li
  • 5,894
  • 6
  • 37
  • 47
1

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.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • Tried that, still got nothing else. – RCIX Sep 02 '10 at 22:36
  • So if you comment out the whole constructor except the trace it doesn't work? – Preet Sangha Sep 02 '10 at 22:50
  • Further research indicates that if i either extend DisplayObject (don't even have to call `super();`) or leave in the line `screenBounds = Lib.current.getBounds(new DisplayObject());` then it fails. – RCIX Sep 03 '10 at 00:17
  • In this case I'd suggest that you need to see the API to Lib.current.getBounds how you can pass in the current instance of Game – Preet Sangha Sep 03 '10 at 00:34
  • THe API reference is no help -- http://haxe.org/api/flash/movieclip (search for getBounds). I'm just trying to get the screen bounds; maybe i should split this into multiple separate questions? – RCIX Sep 03 '10 at 01:09
  • Ok, changing that line to `screenBounds = Lib.current.getBounds(Lib.current);` works. I guess this question is closed, and i have others i'll ask. – RCIX Sep 03 '10 at 01:37
  • Or even better update your question with this information and make the question this one. – Preet Sangha Sep 03 '10 at 01:37
  • I have other unrelated ones, but i will do that for the other half of the issue. – RCIX Sep 03 '10 at 02:10
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;
}
Justinfront
  • 472
  • 2
  • 10