Actionscript: call a superclass method from a subclass
What I would like to do is to call a method from a superclass in a subclass. Specifically I want to be able to add the subclass as a child of the superclass but without physically having to type addChild in the superclass (but I will have to type it in the subclass). For now I'm just trying to call a method in the superclass that draws some text from a subclass.
Here is the MAIN class (the superclass)
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class MAIN extends Sprite
{
public var SOMETEXT:TextField = new TextField();
public function MAIN()
{
new OBJECT_square().CREATE();
}
开发者_高级运维 public function DRAWTEXT():void
{
SOMETEXT.text = "sometext";
addChild(SOMETEXT);
}
}
}
Here is the OBJECT_square class (the subclass)
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends MAIN
{
public function CREATE():void
{
MAIN.DRAWTEXT();
}
}
}
The code doesn't compile, I get "Call to a possibly undefined method DRAWTEXT through a reference with a static type class".
I realize there are otherways to display text on the screen. I just need to learn how to call superclass methods.
Just call it regularly. When you have a class extend a base class, the class inherits all the base classes methods.
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends MAIN
{
public function CREATE():void
{
DRAWTEXT();
}
}
}
Edited
My bad about suggesting static
, I clearly didn't pay much attention to your code before answering. DRAWTEXT doesn't show up anything because your object has not beed added to the stage. You have to call addChild() to see your diplsay object.
As a side note, don't take this bad, buy you have been asking a lot of questions that show that you lack a basic understanding of how Flash and Actionscript works (and also you seem to be a bit too insistent on having it work the way you want).
It's always ok to ask questions here (as long as you keep them on-topic, which seems to be the case with your questions), but I think you would be better off first learning the basics from some good books, tutorials, etc.
These two posts contain links to good resources, I think you should check them out.
Resources for Learning ActionScript 3.0 as a Professional Programmer
https://stackoverflow.com/questions/168586/where-to-learn-actionscript-3-0
Also, what's with all these users with the same name? Right now I can see 9 users with the name 1101
here: https://stackoverflow.com/users/, and I think all of them are yours. One is enough. Really. You don't need to create a new user everytime you want to ask a question. Also, try to follow up the questions you've asked to give some feedback to the people that bothered answering (this will be much easier if you have just one user). And when you want to comment on some response, leave a comment instead of adding an answer.
精彩评论