I’m fairly new to Actionscript 3.0, and I’m having some trouble with this error. I’m currently setting up a small Space Shooter game and am stuck at this error. All was working fine until I started to code the movement class.
Error:
GameScreen
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at GameScreen()
at Main/showMenu()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at TitleScreen/clickPlay()
[CENTER]Code[/CENTER]
Main(this is the document class):
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
public var screen_mc:MovieClip;
public function Main()
{
screen_mc = new TitleScreen();
screen_mc.addEventListener(TitleScreen.PLAY, showMenu); //folder ScreenTitle.<here>
screen_mc.addEventListener(TitleScreen.GAMEOPTIONS, showOps);
addChild(screen_mc);
}
public function showMenu(e:Event) //from dispatch (new Event)
{
screen_mc.removeEventListener(TitleScreen.PLAY, showMenu);
removeChild(screen_mc);
screen_mc = new GameScreen();
screen_mc.addEventListener(GameScreen.PAUSE, gamePause);
screen_mc.addEventListener(GameScreen.WIN, gameWin);
screen_mc.addEventListener(GameScreen.LOSE, gameLose);
addChild(screen_mc);
}
public function showOps(e:Event)
{
screen_mc.removeEventListener(TitleScreen.GAMEOPTIONS, showMenu);
removeChild(screen_mc);
screen_mc = new OptionsScreen();
addChild(screen_mc);
}
public function gamePause(e:Event)
{
screen_mc.removeEventListener(GameScreen.PAUSE, showMenu);
removeChild(screen_mc);
screen_mc = new PauseMenu();
addChild(screen_mc);
}
public function gameWin(e:Event)
{
screen_mc.removeEventListener(GameScreen.WIN, gameWin);
removeChild(screen_mc);
screen_mc = new WinScreen();
addChild(screen_mc);
}
public function gameLose(e:Event)
{
screen_mc.removeEventListener(GameScreen.LOSE, gameLose);
removeChild(screen_mc);
screen_mc = new LoseScreen();
addChild(screen_mc);
}
}
}
TitleScreen.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class TitleScreen extends MovieClip
{
public static const PLAY:String = "play";
public static const GAMEOPTIONS:String = "option";
public function TitleScreen()
{
startMC.addEventListener( MouseEvent.CLICK, clickPlay);
optionsMC.addEventListener( MouseEvent.CLICK, clickOptions);
startMC.buttonMode = true;
optionsMC.buttonMode = true;
}
public function clickPlay(e:MouseEvent)
{
dispatchEvent(new Event( PLAY ));
}
public function clickOptions(e:MouseEvent)
{
dispatchEvent(new Event( GAMEOPTIONS ));
}
}
}
GameScreen(this is where the problem is most likley):
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
public class GameScreen extends MovieClip
{
public static const PAUSE:String = "pause";
public static const WIN:String = "win";
public static const LOSE:String = "lose";
public var rightKey:Boolean = false;
public var leftKey:Boolean = false;
public var movementX:int = 25;
public function GameScreen()
{
trace("GameScreen")
stage.addEventListener( KeyboardEvent.KEY_DOWN, keyPressed );
stage.addEventListener( KeyboardEvent.KEY_UP, keyReleased );
stage.addEventListener( Event.ENTER_FRAME, animate);
stage.addEventListener( MouseEvent.MOUSE_MOVE, crosshair );
//addEventListener( Event.ENTER_FRAME, collisionCheck);
}
public function keyPressed (e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
leftKey = true;
break;
case Keyboard.RIGHT:
rightKey = true;
break;
}
}
public function keyReleased (e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
leftKey = false;
break;
case Keyboard.RIGHT:
rightKey = false;
break;
}
}
public function animate (e:Event)
{
if ( leftKey )
{
ship_mc.x += -movementX;
}
if ( rightKey )
{
ship_mc.x += movementX;
}
}
public function crosshair (e:Event):void
{
//mcCrosshair.gotoAndPlay(1);
Mouse.hide();
mcCrosshair.x = mouseX;
mcCrosshair.y = mouseY;
}
}
}
I was told that this was being caused due to the fact that I was calling stage from a class other than my Document class, meaning that it didn’t know what stage was. That’s understandable, but what else could I possibly do to get this to work?
If I just have addEventListener and no stage. or if I have this.addEventListener, there will be no errors, but there will also be no movement.
Any help is appreciated as I’m still new at this.
Thanks.