Copy link to clipboard
Copied
Hello, I have got a little game already and it works fine, but when i try and break it up into multi-classes It keeps giving me problems i have spend the last 2 days on this and still dont understand what i am doing wrong. Can you help me please. Here is my code.
---So what is going on is i just have a titlemenu class that i want to have everything i do in the title menu go on there when i try and add the event listener to the class it alwasy breaks saying its a null reference but i dont understand why that could be happneing.
package
{
import flash.display.MovieClip;
import com.natejc.input.KeyboardManager;
import com.natejc.input.KeyCode;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.DynamicPropertyOutput;
import flash.text.TextField;
/**
* ...
* @author ...
*/
public class TitleScreen extends MovieClip
{
public var btnStart :SimpleButton;
public function TitleScreen()
{
this.btnStart.addEventListener(MouseEvent.CLICK, btnStartClick);
}
/* ---------------------------------------------------------------------------------------- */
public function btnStartClick(ev:MouseEvent):void
{
gotoAndStop("Game");
}
}
}
****Main class *******
package
{
import com.natejc.input.KeyboardManager;
import com.natejc.input.KeyCode;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.DynamicPropertyOutput;
import flash.text.TextField;
/**
* Drives the project.
*
* @author Nate Chatellier
*/
public class Main extends MovieClip
{
public var tTitleScreen:TitleScreen=new TitleScreen();
public var gGameScreen:GameScreen=new GameScreen();
/* ---------------------------------------------------------------------------------------- */
/**
* Constructs the Main object.
*/
public function Main()
{
KeyboardManager.init(this.stage);
}
}
}
Here are some things I notice:
Copy link to clipboard
Copied
is this the line that causes the null reference error?
this.btnStart.addEventListener(MouseEvent.CLICK, btnStartClick);
if so, there's no btnStart that exists when that line of code executes.
Copy link to clipboard
Copied
There could be, if btnStart is placed on the stage in the symbol that has TitleScreen as its base class.
Copy link to clipboard
Copied
then how could there be null reference error when that line executes?
Copy link to clipboard
Copied
My guess is he separated out the logic into Classes and didn't move the assets the logic references into symbols that have those Classes as Base Classes. However, he's not really all that clear about what he has and exactly what line is getting a npe.
Copy link to clipboard
Copied
That is the problem i am having, but When i have this all under 1 class * main * it works. I have it on the stage and name it so i can connect it to the reference in as3, but when I do it this way it doesnt find the btn now ?
Copy link to clipboard
Copied
Here are some things I notice: