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

  • SEARCH
  • Home
  • 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 8300831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:42:01+00:00 2026-06-08T16:42:01+00:00

I have two classes; my Main class and a class called BlockPlace. I want

  • 0

I have two classes; my Main class and a class called BlockPlace. I want to use my Main class to run BlockPlace, but for some reason it isn’t working. I’ve tried changing both codes, but it only results in errors. I know for a fact that the code works because I tested it in the timeline. Should I restructure the entire thing or is there a different solution? Here are my classes:

Main Class:

package  
{  
    import flash.display.*;
    import source.map.*;

    public class Main extends MovieClip{
        public function Main()  
        {  
            BlockPlace();
        }  
    }  
}

BlockPlace:

package source.map{

    import flash.display.MovieClip;
    import flash.display.Stage;

    public class BlockPlace extends MovieClip{

        public function BlockPlace(){
            var db:MovieClip = new dbox();
            stage.addChild(db);
            db.x = stage.stageWidth / 2;
            db.y = stage.stageHeight / 2;
        }
    }
}
  • 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-06-08T16:42:06+00:00Added an answer on June 8, 2026 at 4:42 pm

    You’ve got a couple of problems that I can see, which might explain the behavior you are seeing.

    First, you are calling this like is a function, not a constructor.

    If you want to statically reference this function, then you want to change things up a bit:

    package source.map{
    
        import flash.display.Stage;
    
        public class BlockPlace {
            public static function create(stage:Stage){
                var db:MovieClip = new dbox();
                stage.addChild(db);
                db.x = stage.stageWidth / 2;
                db.y = stage.stageHeight / 2;
            }
        }
    }
    

    And then invoke it using:

    BlockPlace.create(stage);
    

    Notice that the class no longer extends MovieClip, and the method is marked as static, meaning it can be referenced directly from the class, without a instance of the class. Also notice that the functions signature has changed. It now accepts one argument — the stage — that gets passed in from the outside.

    Using this approach, you give the reference to the stage from your Main instance into this utility function to do some work. This is probably the most straight-forward solution to your problem.


    Conversely, if you want to keep this as a class that extends MovieClip — there are some implications of what that means — then you need to instantiate an instance of the class:

    var blockPlace:BlockPlace = new BlockPlace();
    

    Then, you need to get it on the sage:

    this.addChild(blockPlace);
    

    But you’ve still got a bit of a chicken/egg problem with the stage. When you instantiate the class, in its constructor you are referencing the stage property. At this point of its construction however, it hasn’t been added to any DisplayObjectContainers who are attached the stage. So the stage property will be null. As such, you can’t add anything to the stage or get the stageHeight or stageWidth.

    You’ll need to wait until that has happened — which wont happen until addChild(blockPlace) gets evaulated.

    So you need to re-think your architecture a bit, you can either move the logic into another function that will get explicitly called by you once you are sure that its has been attached to the stage. Something like:

    package source.map{
    
        import flash.display.Stage;
    
        public class BlockPlace {
            public function BlockPlace() {
    
            }
    
            public function initialize():void {
                var db:MovieClip = new dbox();
                stage.addChild(db);
                db.x = stage.stageWidth / 2;
                db.y = stage.stageHeight / 2;
            }
        }
    }
    

    And then consumed using something like :

    var blockPlace:BlockPlace = new BlockPlace();
    addChild(blockPlace);
    blockPlace.initialize(); // safe to call because its on the stage now given that `this` is on the stage
    

    A more idiomatic approach however would be to use events to wait until the MovieClip has been added to the stage, and then perform some work. For example:

    package source.map{
    
        import flash.display.Stage;
    
        public class BlockPlace {
            public function BlockPlace() {
                this.addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
    
            }
    
            private function stageAvailable(e:Event):void {
                var db:MovieClip = new dbox();
                stage.addChild(db);
                db.x = stage.stageWidth / 2;
                db.y = stage.stageHeight / 2;
            }
        }
    }
    

    Notice that the constructor now sets up an event listener which says, “When I get added to the stage, go do the work that’s in this other function called stageAvailable.”

    There are some minor caveats that you have to be aware of, it is possible for display objects to be constructed directly on the stage — the flash IDE does this. so the event will not fire for those items, because by the time the event gets attached, its already on the stage, and thus will not fire. The most defensive way to write it is this:

    package source.map{
    
        import flash.display.Stage;
    
        public class BlockPlace {
            public function BlockPlace() {
                if(stage) stageAvailable()
                else this.addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
    
            }
    
            private function stageAvailable(e:Event = null):void {
                if(event) this.removeEventListener(Event.ADDED_TO_STAGE, stageAvailable);
                var db:MovieClip = new dbox();
                stage.addChild(db);
                db.x = stage.stageWidth / 2;
                db.y = stage.stageHeight / 2;
            }
        }
    }
    

    In the constructor, it checks to see if there is a stage, and if so then it immediately calls the function. If not, then it sets up a listener to wait until that happens. Then in the handler, if it was called because of an event, then clean up after itself and remove the listener.

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

Sidebar

Related Questions

So I have two classes. One is a class called Main. The Main class
I have a main class and two extended classes: class Main { public $foo;
I have two classes orchestrated by a main class and I would like to
I have two java classes. Schedule is the main class that uses an array
First I want to describe my situation briefly. I have two classes, one MainClass
I'd like to have two main classes (or more) with leiningen, and then be
Starting to learn Canvas and have two classes so far (main one to call
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some

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.