• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Just Starting AS3 Not understanding Multi-class interaction

New Here ,
Apr 15, 2014 Apr 15, 2014

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);

                    }

          }

}

TOPICS
ActionScript

Views

470

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Apr 15, 2014 Apr 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 th
...

Votes

Translate

Translate
Community Expert ,
Apr 15, 2014 Apr 15, 2014

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 15, 2014 Apr 15, 2014

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 15, 2014 Apr 15, 2014

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines