I ran into the stage access problem in a project I’m working on and, rather than passing on the actual project, I’ve simplified it in a smaller test project to illustrate my problem.
Here’s the .fla:
var greenSquare:Sprite = new Sprite();
greenSquare.graphics.beginFill(0x00FF00);
greenSquare.graphics.drawRect(-30, -30, 60, 60);
greenSquare.graphics.endFill();
addChild(greenSquare);
greenSquare.x = stage.stageWidth/2;
greenSquare.y = stage.stageHeight/2;
var littleSquare1:Sprite = new Sprite();
littleSquare1.graphics.beginFill(0xFF0000);
littleSquare1.graphics.drawRect(-10, -10, 20, 20);
littleSquare1.graphics.endFill();
addChild(littleSquare1);
littleSquare1.x = 475;
littleSquare1.y = 200;
littleSquare1.addEventListener(MouseEvent.CLICK, moveToLittleSquare1);
function moveToLittleSquare1(e:MouseEvent) {
blueSquare.x = littleSquare1.x;
blueSquare.y = littleSquare1.y;
}
var littleSquare2:Sprite = new Sprite();
littleSquare2.graphics.beginFill(0xFF0000);
littleSquare2.graphics.drawRect(-10, -10, 20, 20);
littleSquare2.graphics.endFill();
addChild(littleSquare2);
littleSquare2.x = 275;
littleSquare2.y = 350;
littleSquare2.addEventListener(MouseEvent.CLICK, moveToLittleSquare2);
function moveToLittleSquare2(e:MouseEvent) {
blueSquare.x = littleSquare2.x;
blueSquare.y = littleSquare2.y;
}
var littleSquare3:Sprite = new Sprite();
littleSquare3.graphics.beginFill(0xFF0000);
littleSquare3.graphics.drawRect(-10, -10, 20, 20);
littleSquare3.graphics.endFill();
addChild(littleSquare3);
littleSquare3.x = 75;
littleSquare3.y = 200;
littleSquare3.addEventListener(MouseEvent.CLICK, moveToLittleSquare3);
function moveToLittleSquare3(e:MouseEvent) {
blueSquare.x = littleSquare3.x;
blueSquare.y = littleSquare3.y;
}
var littleSquare4:Sprite = new Sprite();
littleSquare4.graphics.beginFill(0xFF0000);
littleSquare4.graphics.drawRect(-10, -10, 20, 20);
littleSquare4.graphics.endFill();
addChild(littleSquare4);
littleSquare4.x = 275;
littleSquare4.y = 50;
littleSquare4.addEventListener(MouseEvent.CLICK, moveToLittleSquare4);
function moveToLittleSquare4(e:MouseEvent) {
blueSquare.x = littleSquare4.x;
blueSquare.y = littleSquare4.y;
}
var blueSquare:Sprite = new Sprite();
blueSquare.graphics.beginFill(0x0000FF);
blueSquare.graphics.drawRect(-30, -30, 60, 60);
blueSquare.graphics.endFill();
addChild(blueSquare);
blueSquare.x = littleSquare1.x;
blueSquare.y = littleSquare1.y;
var movingDot:DotMove;
stage.addEventListener(KeyboardEvent.KEY_DOWN, startDot);
function startDot(evt:KeyboardEvent):void {
if (evt.keyCode == 32) {// spacebar
go();
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, stopDot);
function stopDot(evt:KeyboardEvent):void {
if (evt.keyCode == 17) {// CTRL key
removeChild(movingDot);
}
}
function go():void {
movingDot = new DotMove(blueSquare.x, blueSquare.y, greenSquare.x, greenSquare.y);
addChild(movingDot);
}
Here’s DotMove.as:
package {
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
public class DotMove extends Sprite {
private var dot:Sprite;
private var step:Number = 0.1;
private var timer:Timer;
private var x1:Number;
private var y1:Number;
private var x2:Number;
private var y2:Number;
public function DotMove(x1, y1, x2, y2):void {
init(x1, y1, x2, y2);
}
function init(x1, x2, y1, y2) {
dot = new Sprite();
dot.graphics.beginFill(0x000000);
dot.graphics.drawCircle(-2, -2, 4);
dot.graphics.endFill();
addChild(dot);
dot.x = x1;
dot.y = y1;
timer.addEventListener(TimerEvent.TIMER, translate);
timer.start();
}
function translate(e:TimerEvent):void {
trace("in translate");
}
}
}
The actual functionality I’m looking for is, when I hit the spacebar, the dot moves from the blue square to the green square. I’ve left out that code and only included a trace.
When I hit the spacebar, I get this standard error: “Error #1009: Cannot access a property or method of a null object reference.” My problem is this:
I know that I can get access to the stage in the DotMove class by changing this…
public function DotMove():void {
init();
}
function init() {
…to this…
public function DotMove():void {
addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(event:Event) {
My problem is that I need to pass (x1, x2, y1, y2) to DotMove.
How can I pass (x1, x2, y1, y2) to DotMove, and to init, if init’s argument is already (event:Event)?
Thanks!