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

How do you addChild from a class?

Contributor ,
Jun 15, 2011 Jun 15, 2011

Copy link to clipboard

Copied

Hello, I'm trying to have a class file which can add objects to the stage via addChild; however, when I call addChild from within the class file, I get the error "1180: Call to a possibly undefined method addChild." I've tried importing flash.display.* and that doesn't fix the problem. Does the class file have to extend Sprite or MovieClip to be able to add objects to the stage?

TOPICS
ActionScript

Views

1.3K

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

Mentor , Jun 16, 2011 Jun 16, 2011

A sample document class:

package  {
    import flash.display.MovieClip;
   
    public class test extends MovieClip {
        public function test() {
            // constructor code
            var test:MovieClip=new t();
            addChild(test);
           
        }
    }
}

t is the movieclip which have t as a class name in the library.

Votes

Translate

Translate
Community Expert ,
Jun 15, 2011 Jun 15, 2011

Copy link to clipboard

Copied

your class instance must extend the displayobject.  so yes, extending sprite or movieclip would work.

or you can pass a displayobject reference to your class and apply the addChild() method to that reference.

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
Mentor ,
Jun 16, 2011 Jun 16, 2011

Copy link to clipboard

Copied

A sample document class:

package  {
    import flash.display.MovieClip;
   
    public class test extends MovieClip {
        public function test() {
            // constructor code
            var test:MovieClip=new t();
            addChild(test);
           
        }
    }
}

t is the movieclip which have t as a class name in the library.

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
Contributor ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

All right, thank you, that's very helpful.

Another related question: is it possible to have a class that extends MovieClip but uses a specific graphic? Er, to better explain, say I have in my library:

A MovieClip named MCBox

A Class named ClassBox which Extends MovieClip

Is it possible to make it so if I make a new instance of ClassBox and add it to the stage, it uses the MCBox graphic? And I can just change the x,y, etc like normal (i.e. ClassBox.x=25)? Or would I have to make the MCBox a property, and refer to it like "ClassBox.image.x=25" or similarly?

Really appreciate the help! Sorry for not responding sooner, I've been very busy.

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 ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

right click your library movieclip, tick export for actionscript and assign a class (eg, ClassBox) that matches your class file.  when you create an instance of ClassBox:

var cb:ClassBox=new ClassBox();

the constructor in your class file will execute.

and yes, you can use

cb.x=

cb.y=

where you create your cb instance or you can assign those properties in your class file (or do both).

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
Contributor ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Oooh, I never knew that. Thanks!

EDIT: The constructor in my custom class file is supposed to take an argument, but if I follow the format var cb:ClassBox=new ClassBox(argument), I get the wrong number of arguments (expected 0) error, so I don't think it's calling the correct constructor. Any thoughts?


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 ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

your class isn't being used because of a typo or path issue.

show your class code from package{  to the end of your constructor.

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
Contributor ,
Jul 17, 2011 Jul 17, 2011

Copy link to clipboard

Copied

There's no typo or path issue, if I instance the custom class separately it works fine. Could you explain in more detail how this is supposed to work?

edit: Never mind, I didn't realize from your explanation that the Class has to have the same name as the MovieClip >_< . It didn't complain when I tied it to a class with a different name, and it worked if I didn't try to pass it any parameters, so I was confused.

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 ,
Jul 17, 2011 Jul 17, 2011

Copy link to clipboard

Copied

LATEST

if this issue is resolved, please mark helpful/correct responses, if there are any.

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