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 522165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:19:34+00:00 2026-05-13T08:19:34+00:00

I’m wondering (based on scoping rules) how I might do the following: I want

  • 0

I’m wondering (based on scoping rules) how I might do the following:
I want to draw to a sprite that exists on the main stage in which I have a class instantiated.

So something like

public function MyClass(reference:String){
   this.reference = reference;
}

public function drawToOutsideSprite(){
   this.parent.getChildByName(this.reference).addChild(someLoaderName);
}

Would I use super() in this case, or what’s the usual methodology?

Thanks,
jml

  • 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-13T08:19:34+00:00Added an answer on May 13, 2026 at 8:19 am

    There are a few ways to do this. I’m assuming your MyClass extends Sprite.

    
    package
    {
        import flash.display.DisplayObject;
        import flash.display.DisplayObjectContainer;
        import flash.display.Sprite;
        /**
         *  MyClass
         */
        public class MyClass extends Sprite
        {
            public var referenceA:String;
            public var referenceB:Sprite;
    
            public function get referenceA_way2():Sprite
            {
                return this.parent.getChildByName(referenceA);
            }
            /**
             *  MyClass Constructor
             */
            public function MyClass(referenceA:String = null, referenceB:Sprite = null)
            {
                super();
                this.referenceA = referenceA;
                this.referenceB = referenceB;
            }
    
            public function drawToOutsideSpriteA(child:DisplayObject):void
            {
                // referenceA
                this.parent.getChildByName(this.referenceA).addChild(child);
                // or
                referenceA_way2.addChild(child);
            }
    
            public function drawToOutsideSpriteB(child:DisplayObject):void
            {
                // referenceB
                referenceB.addChild(child);
            }
    
            public function drawToOutsideSpriteC(referenceC:String, child:DisplayObject):void
            {
                this.parent.getChildByName(referenceC).addChild(child);
            }
    
            // Do this:
            // it allows you to abstract out the logic of getting the main sprite
            // into some util class, so you could reuse that functionality elsewhere,
            // and so your code is cleaner.
            public function drawToOutsideSpriteD(child:DisplayObject):void
            {
                StageUtil.getMainSprite().addChild(child);
            }
        }
    }
    
    package
    {
        import flash.display.DisplayObject;
        import flash.display.DisplayObjectContainer;
        import flash.display.Sprite;
        /**
         *  MyClass
         */
        public class StageUtil
        {
            private static var root:Stage;
    
            /**
             *  Called when app first starts
             */
            public static function initialize(stage:Stage):void
            {
                root = stage;
            }
    
            public static function getMainSprite():DisplayObjectContainer
            {
                return root; // or something more complex,
                // like a recursive function to getSpriteByName
            }
    
            public static function addToStage(child:DisplayObject):DisplayObject
            {
                return getMainSprite().addChild(child);
            }
        }
    }
    

    In general I would abstract out the logic for getting the “main” sprite into some util/manager class, because you don’t want to hardcode that into your MyClass, as you might need it in other places, and you might want to customize it later on. It sounds like your just asking what’s the best way to reference sprites outside of the scope of the MyClass, so I say just put it into the Util, assuming it has good reason for being their (like FlexGlobals.topLevelApplication in Flex, so you can easily access the application).

    I don’t recommend passing in id‘s or name‘s into the constructor and doing it that way, I don’t really recommend constructor arguments at all. I would just pass those into a method if you needed to, or have it built into the class itself, or the Util.

    To clear up the scoping question a little… You normally don’t want to draw to sprites outside the scope of the class you are in, unless they have some special functionality that will be referenced by multiple classes with totally different scopes. This is because things would start not making sense, who’s being added to who. But some good examples on when to do thatinclude:

    • Buttons with ToolTips: Tooltips are added to the root because they appear on top of everything, but a Button could be 20 children deep, so you’d have in the Button subclass, perhaps, addToolTip(child).
    • PopUps: You might want to add a popup from within MyClass, but it’s really being added to the stage. In flex this is like PopUpManager.addPopUp(child), just like the sample StageUtil.getMainSprite().addChild(child). You could even wrap that method so it’s like the one in the class above, addToStage.
    • Transform/Drawing Stage: If you have some global painting stage, or place where you scale/resize things, you might want to be able to add/remove graphics from that from any class.

    The super() method isn’t useful in this scenario. The only time you really use super() is if you have overridden a method, and want to access the super-classes implementation. Something like this (assuming you’re extending Sprite):

    
    override public function addChild(child:DisplayObject):DisplayObject
    {
        if (child is MyDrawingSprite)
            return StageUtil.addToStage(child); // add to main stage
        else
            return super.addChild(child); // add directly to this class
    }
    

    Otherwise, try to stick to just adding children directly to the “MyClass”.

    Hope that helps.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.