Reputation: 21
I have a problem when I create a menu with flash as3. Can you guys help what should I do ...? Like this :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DocumentClass/menuScreen()
at DocumentClass/mouseBack()
how to right code to fix the code below:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.StageDisplayState;
public class DocumentClass extends MovieClip {
public var backbutton:BackButton = new BackButton();
public function DocumentClass() {
menuScreen();
stage.addEventListener(Event.ENTER_FRAME, masukGame);
}
public function masukGame(e:Event):void{
stage.displayState = StageDisplayState.FULL_SCREEN;
}
public function menuScreen():void{
game.menuscreen.mb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick1);
game.menuscreen.pb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick2);
game.menuscreen.mlb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick3);
game.menuscreen.kb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick4);
}
public function backMenu():void{
backbutton.buttonMode = true;
backbutton.addEventListener(MouseEvent.MOUSE_DOWN, mouseBack);
backbutton.x = 450;
backbutton.y = 400;
// Dan akhirnya letakan button ke stage;
addChild(backbutton);
backbutton.visible=true;
}
public function mouseClick1(event:MouseEvent):void {
game.gotoAndStop("MainGame");
backMenu();
}
public function mouseClick2(event:MouseEvent):void {
game.gotoAndStop("Petunjuk");
backMenu();
}
public function mouseClick3(event:MouseEvent):void {
game.gotoAndStop("Malaria");
backMenu();
}
public function mouseClick4(event:MouseEvent):void {
game.gotoAndStop("Keluar");
backMenu();
}
public function mouseBack(event:MouseEvent):void {
menuScreen();
}
}
}
And this is my file, download here: https://www.dropbox.com/s/jo7bcwnlfj7qjnh/My%20Game.rar
Please help me, and thanks guys ...
Upvotes: 1
Views: 9978
Reputation: 1
The solution is to change the flash to the Version of another kind. I experimented with it and the problem went away.
Upvotes: 0
Reputation: 2098
Your problem is a Null Pointer Exception.
It means that you are trying to access a variable inside a null object.
The StackTrace you provided guide us to the problem.
The last place in the trace are:
DocumentClass/menuScreen()
So, your problem is inside the menuScreen()
of the DocumentClass
class.
public function menuScreen():void
{
game.menuscreen.mb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick1);
game.menuscreen.pb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick2);
game.menuscreen.mlb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick3);
game.menuscreen.kb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick4);
}
Here we try to access:
game
game.menuscreen
game.menuscreen.mb
game.menuscreen.pb
game.menuscreen.mlb
game.menuscreen.kb
One of this are null.
EDIT 1
Ok, I tried your code .fla and found out that the problem is the game.menuscreen
call.
Once you change your screen you remove the menuscreen
from the stage.
When you get back and try to access it, it doesn't exist anymore, so, you get an NPE (Null Pointer Exception).
EDIT 2
You can solve the problem with different approaches, but the easiest (and least flexible) is to keep a reference to your menuscreen
somewhere you can get it latter.
E.g.: You can keep it in the DocumentClass
class this way:
public class DocumentClass extends MovieClip
{
public var backbutton:BackButton = new BackButton();
public var gameMenuScreen:MovieClip;
public function DocumentClass()
{
gameMenuScreen = game.menuscreen;
menuScreen();
stage.addEventListener(Event.ENTER_FRAME, masukGame);
}
// ... rest of the class
}
Then you can access it directly from the DocumentClass
, this way:
public function menuScreen():void
{
// It was "game.menuscreen"
// Now it is "gameMenuScreen"
gameMenuScreen.mb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick1);
gameMenuScreen.pb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick2);
gameMenuScreen.mlb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick3);
gameMenuScreen.kb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick4);
}
Upvotes: 1