tags:

views:

50

answers:

2

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.

+1  A: 

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
Tried that, still got nothing else.
RCIX
So if you comment out the whole constructor except the trace it doesn't work?
Preet Sangha
Correct. Odd...
RCIX
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
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
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
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
Or even better update your question with this information and make the question this one.
Preet Sangha
I have other unrelated ones, but i will do that for the other half of the issue.
RCIX
+3  A: 

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