Skip to main content
Participant
April 15, 2014
Answered

Just Starting AS3 Not understanding Multi-class interaction

  • April 15, 2014
  • 2 replies
  • 653 views

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;

          /**

           * ...

           * @7111211 ...

           */

          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.

           *

           * @7111211          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);

                    }

          }

}

This topic has been closed for replies.
Correct answer Amy Blankenship

Here are some things I notice:

  1. In your Main Class, you create a new TitleScreen and GameScreen, but you don't add them to the stage.
    • If you are seeing them on stage, these are the ones the Flash Player made for you and not the ones you created.
    • If you have control over them, then that means that the Flash player overwrote the ones you made, so you can probably drop out the part where you made them.
    • If you do not have control over them, then the lines where you created them ran after the part of the constructor where Flash added them to the stage (unlikely)
    • If you do not see them, then you need to add them with addChild().
  2. It seems to me unlikely that your TitleScreen MC includes the label "game," so I think you should be getting an error if that line runs. As Kglad says, you might be getting an error if you don't have an instance called btnStart on your stage. You should probably be dispatching a custom, bubbling event from TitleScreen and simply listen for that from Main and then control whoever really does contain that game label.
  3. I think you'll regret making your keyboard event listener be static, since that means you can't have more than one (i.e. you could potentially have different ones that operate at different parts of the game or in your next title you might want to write a different implementation). You don't show the implementation of that Class, but I generally pass the stage to such Classes typed as an IEventDispatcher to limit how much that Class needs to know. I'll then also give it a different IEventDispatcher to broadcast the "system" events that map to specific keystrokes. Hopefully you're not giving something responsible for listening keyboard events actual management authority, especially since it then needs to find your Main Class and figure out if it has the properties and methods it needs.
  4. If you use Flash Builder, you can get rid of all those import statements that are for Classes you're not using by using Ctrl-Shift-O.

2 replies

Amy Blankenship
Amy BlankenshipCorrect answer
Legend
April 15, 2014

Here are some things I notice:

  1. In your Main Class, you create a new TitleScreen and GameScreen, but you don't add them to the stage.
    • If you are seeing them on stage, these are the ones the Flash Player made for you and not the ones you created.
    • If you have control over them, then that means that the Flash player overwrote the ones you made, so you can probably drop out the part where you made them.
    • If you do not have control over them, then the lines where you created them ran after the part of the constructor where Flash added them to the stage (unlikely)
    • If you do not see them, then you need to add them with addChild().
  2. It seems to me unlikely that your TitleScreen MC includes the label "game," so I think you should be getting an error if that line runs. As Kglad says, you might be getting an error if you don't have an instance called btnStart on your stage. You should probably be dispatching a custom, bubbling event from TitleScreen and simply listen for that from Main and then control whoever really does contain that game label.
  3. I think you'll regret making your keyboard event listener be static, since that means you can't have more than one (i.e. you could potentially have different ones that operate at different parts of the game or in your next title you might want to write a different implementation). You don't show the implementation of that Class, but I generally pass the stage to such Classes typed as an IEventDispatcher to limit how much that Class needs to know. I'll then also give it a different IEventDispatcher to broadcast the "system" events that map to specific keystrokes. Hopefully you're not giving something responsible for listening keyboard events actual management authority, especially since it then needs to find your Main Class and figure out if it has the properties and methods it needs.
  4. If you use Flash Builder, you can get rid of all those import statements that are for Classes you're not using by using Ctrl-Shift-O.
kglad
Community Expert
Community Expert
April 15, 2014

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.

Amy Blankenship
Legend
April 15, 2014

There could be, if btnStart is placed on the stage in the symbol that has TitleScreen as its base class.

kglad
Community Expert
Community Expert
April 16, 2014

then how could there be null reference error when that line executes?