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.