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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:14:20+00:00 2026-05-25T06:14:20+00:00

I’m unsuccessfully attempting to instantiate a reference of a class that is passed as

  • 0

I’m unsuccessfully attempting to instantiate a reference of a class that is passed as a parameter to another class.

in this example there are 3 classes: MainClass, Canvas, MyCircle

from the MainClass i am creating an instance of Canvas, which is passed a class reference of MyCircle as i want to create instances of MyCircle from within Canvas. however, the MyCircle constructor contains required parameters that are created from within Canvas.

how can i pass and instantiate a class reference with required parameters?

MyCircle:

package
{
    //Imports
    import flash.display.Shape;

    //Class
    public class MyCircle extends Shape
    {
        //Constructor
        public function MyCircle(color:uint, radius:uint)
        {
            graphics.beginFill(color, 1.0);
            graphics.drawCircle(0, 0, radius);
            graphics.endFill();
        }
    }
}

Canvas:

package
{
    //Imports
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    //Class
    public class Canvas extends Sprite
    {
        //Constructor
        public function Canvas(circleClassReference:String, amount:uint)
        {
            var CircleReference:Class = getDefinitionByName(circleClassReference) as Class;

            for (var i:uint = 0; i < amount; i++)
            {
                var randomColor:uint = Math.random() * 0xFFFFFF;
                var randomRadius:uint = Math.random() * 100 + 50;

                var circleInstance:DisplayObject = new CircleReference(randomColor, randomRadius);
                circleInstance.y = i * randomRadius;

                addChild(circleInstance);   
            }
        }
    }   
}

MainClass:

package
{
    //Imports
    import flash.display.Sprite;

    //Class
    public class MainClass extends Sprite
    {
        //Constructor
        public function MainClass()
        {
            var myCanvas:Canvas = new Canvas("MyCircle", 10);
            addChild(myCanvas)
        }
    }
} 

[EDIT]


it seems that passing the actual Class instead of the name of the class avoids the need for the dirty fix that is mentioned below.

MainClass Constructor

public function MainClass()
{   
    var myCanvas:Canvas = new Canvas(MyCircle, 10);
    addChild(myCanvas)
}

Canvas Constructor

public function Canvas(circleClassReference:Class, amount:uint)
{
    var CircleReference:Class = circleClassReference;

    for (var i:uint = 0; i < amount; i++)
    {
        var randomColor:uint = Math.random() * 0xFFFFFF;
        var randomRadius:uint = Math.random() * 100 + 50;

        var circleInstance:DisplayObject = new CircleReference(randomColor, randomRadius);
        circleInstance.y = i * randomRadius;

        addChild(circleInstance);   
    }
}
  • 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-25T06:14:20+00:00Added an answer on May 25, 2026 at 6:14 am

    Importing (or being in the same package) isn’t enough to make the compiler include a class definition in the Application Domain. You will actually have to use the class you want somewhere, or embed the definition in an SWF that you load in. This should fix your error though:

    package
    {
        //Imports
        import flash.display.Sprite;
    
        //Class
        public class MainClass extends Sprite
        {
            //Constructor
            public function MainClass()
            {
                //unused variable definition for class embedding purposes
                var tempCircle:MyCircle;
                var myCanvas:Canvas = new Canvas("MyCircle", 10);
                addChild(myCanvas)
            }
        }
    }
    

    While this will prevent MyCircle from being undefined later on, I’ve always thought of it as a bit of a dirty fix (despite having it in some fairly major projects). I’d love to know a better solution, other than embedding the definition in a separate SWF.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I am doing a simple coin flipping experiment for class that involves flipping a
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.