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?
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.
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.
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.
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.
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).
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?
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.
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.
Copy link to clipboard
Copied
if this issue is resolved, please mark helpful/correct responses, if there are any.