Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:24:36+00:00 2026-05-23T19:24:36+00:00

I am new to AS3 and am trying to lean its OOP ways. What

  • 0

I am new to AS3 and am trying to lean its OOP ways. What I am having problems with is understanding how to access the stage with separate classes.

Here is an example of what I am trying to do:

package game{
import flash.display.*;

public class Main extends MovieClip{

    function Main(){
        var player = new Player();
        var playerBullets = new playerBullet();
        addChild(player.players);
    }

}

package game{
import flash.display.*;

public class Bullet extends Main // also tried with MovieClip and Sprite{

    function Bullet(){
        // empty
    }


    function blah(){
        var someSprite = new someSprite();

        Main.addChild(someSprite);
        stage.addChild(someSprite);
        root.addChild(someSprite);


    }
}
}

I have Omitted another class which calls the blah method as I feel it is not relevant.

Basically what I want to know is how to add things to the stage in classes as it lookes like I am missing something crucial.

*EDIT TO INCLUDE ERROR*

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game::Bullet/blah()
at game::Player/fire()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:24:38+00:00Added an answer on May 23, 2026 at 7:24 pm

    You shouldn’t necessarily be extending main to create something like a bullet class, this can be it’s own class that extends Sprite or MovieClip. The stage object is considered a global object, as it is a singleton (except in the case of Adobe AIR where you can have one stage per NativeWindow that you spawn). So any object that extends DisplayObject or has DisplayObject in it’s inheritance chain will by default have a reference to the stage via a getter, which is populated automatically when a displayObject is added to the display list. This can happen by either adding a clip directly to the root stage object or by adding a clip as a child of another clip, that eventually connects to the stage. For example:

    var clip1:MovieClip = new MovieClip();
    
    stage.addChild(clip1); //Clip 1 can now access the stage reference internally.
    
    ver clip2:MovieClip = new MovieClip(); //Right now, clip2 cannot access the stage reference interally.
    
    clip1.addChild(clip2); //Now clip2 can access the internal stage reference because it has been connected to the display list through clip1.
    

    The other mistake people make is accessing stage within a DisplayObject typed class (such as your Main class) without first ensuring that the object itself has been added to the stage. You do this by listening for the Event.ADDED_TO_STAGE event within the constructor of the class, like so:

    public class Main extends MovieClip{
    
        function Main(){
            if(stage){
                //The stage reference is present, so we're already added to the stage
                init();
            }else{
                addEventListener(Event.ADDED_TO_STAGE, init);
            }
            var player = new Player();
            var playerBullets = new playerBullet();
            addChild(player.players);
        }
    
        private function init(e:Event = null)
        {
             trace("Added to stage, the stage reference is now populated and stage can be accessed");
        }
    
    }
    

    This could be the problem you’re having, but it’s hard to say since you have not specified any errors. However, this is likely an issue or will be for you, since it’s quite common. Inside the init() method you can then set a flag so that when external classes call your Main.blah() method, you can ensure that the stage reference exists before attempting to add something to the stage. Take note however that within your Main class when you simply say:

    addChild(someChild);

    or

    this.addChild(someChild);

    you’re not adding that child to the stage, but rather to the Main object, which is a MovieClip or Sprite based object that is itself attached to the stage automatically when you set it as the Document class. Hope this info helps.

    Update
    To explain the display list a little more:

    Think of all your movieclips as dishes, and the stage as the table. You can only access the table from the dish, if the dish is placed directly on the table, or if a dish is stacked on top of another dish that touches the table. If you have 10 plates stacked on top of each other, they all touch the table eventually, via their connection to each other. This is essentially a visualization of the flash display list. The way you put dishes on the table is by using addChild(dish). If you have not placed an object somewhere on the table, and try to access the table from that object, you’re going to fail. You’re getting the “access to undefined” error because you’re calling the “blah()” method, which accesses the stage (table) before the bullet (dish) has been added to the stage (table). So you must first either directly add the bullet to the stage, or add it to another object that has already been added to the stage. Change your code like so:

    var myBullet:Bullet = new Bullet();
    stage.addChild(myBullet);
    //Or, if this class, assuming it's the player class, has already been added to the stage, just do this:
    this.addChild(myBullet);
    myBullet.blah();
    

    Even so, you should still have some error checking within your “blah” method to ensure that the stage is available:

      function blah(){
            var someSprite = new someSprite();
            if(stage){
                Main.addChild(someSprite);
                stage.addChild(someSprite);
                root.addChild(someSprite);
            }else{
                trace("error, stage not present");
            }
        }
    

    However you should also note that by adding this child to Main, then stage, then root all in sequence, this does not duplicate the someSprite object. When you add a display object to a new parent object, the object is automatically pulled from it’s current parent and moved to the new one. So all this code will do is eventually add someSprite to root, which I believe will fail because root is not a display object, but rather a global reference mainly used to access global objects such as the stage and the Loader object used to load the SWF.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning as3 and I'm having difficulties understanding events. I'm trying to load options
I'm new to AS3 and I'm getting this error while trying to implement OO
Just going to say that I'm new to AS3, What I'm trying to do
I'm trying to use the new(ish) AS3 global error handling class. I am trying
I'm new to AS3 and I'm trying to create a simple game with it.
Im new to Flash AS3 Game programming,,,There is a problem i'm facing,,,Im trying to
Hello im very new at AS3 so im a litle confused trying to get
newbie on AS3 here! :) basically I'm trying to write an application that let
im having some problems trying to read a SQlite data base from flash using
I'm really new to flash and as3... I'm trying to create 2 scenes in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.