views:

175

answers:

4

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.

+1  A: 

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.

http://stackoverflow.com/questions/2267369/resources-for-learning-actionscript-3-0-as-a-professional-programmer

http://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: http://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.

Juan Pablo Califano
Now that we know what he meant by "superclass" and "subclass" I bet this makes a lot more sense. He was asking how to access the parent container from a child component. The answer being: through the `parent` property.
Gunslinger47
A: 

ok so static doesn't work at all (compiler errors) and when I just use DRAWTEXT nothing shows up.

I have a hard time being taught things, I have to do them which means a lot of guess and check. So sooner or later I'll run into a wall like this one so I ask for help in terms of how I understand it. I do try to look it up but either there's no information or the person explaining it cannot explain things in a way that makes sense to me.

Also didn't realize I was making a new user every time I posted, I thought you just picked a name and posted and didn't have to make a permanent user account if you didn't want to.

This should be a comment, not a new answer.
Juan Pablo Califano
You're right about the user thing. I didn't realize you were not using a permanent account. I'd suggest you create one, though, as it's very easy. (But again, you're right in that this is not necessary, only a convenience)
Juan Pablo Califano
How exactly am I supposed to be using the addChild() then? I thought I was calling it in the DRAWTEXT function. I want to use it from within the subclass. I really don't like the idea of having to do so much stuff from the superclass. I'd rather just make a class and have it declare itself as a child of the superclass.
Just like you were told here: http://stackoverflow.com/questions/3488092/actionscript-add-a-sprite-from-a-subclass-to-the-display-list-of-its-super-class. You created an OBJECT_square. This object itself has not been added to the display list. So it won't be visible, even though you are adding a textfield to it.
Juan Pablo Califano
but the problem I encountered there was that there was no way to update the square. I don't really know how to explain it, but basically I couldn't get the square to move when I pressed a key. Did I handle the keyboard even wrong then?
Yes. You should attach a key_down listener on the stage. Otherwise, your function will only be called when your object is on focus (assuming it has a visible area). Also, your idea of not keeping track of your objects will lead to problems. You have to keep some references to it. Otherwise you'll not be able to remove them later (and do some cleanup, which is necessary if you add listeners on the stage).
Juan Pablo Califano
Actually forget the whole keyboard thing I can worry about that later. As for removing the objects, I'd like for them to be able to remove themselves when necessary. The thing is I NEED a way to use the addChild() command from within a subclass. There has to be some way that I can generate a variable in the square class and then add it to the display list of the main class.
You addChild to another object. So you need a reference to this other object. Normally, you create some display object on some display object container (such as a Sprite) and then call addChild(the_new_object) to add it. If you want to go the other way round, you'll have to pass a reference to the container. A way to do this: from your Sprite you'd do something like `new SomeObject(this)` and in SomeObject: `function SomeObject(container:Sprite) { container.addChild(this); }`.
Juan Pablo Califano
Thank you so much. I finally got it working. All I have to do in the main class is tell it to make a square and then the square handles drawing and moving itself.
+1  A: 

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();
        }
    }
}
Fox
A: 

this.method(); because you are extending it... so it has all the methods the parent has PLUS what you define in it. Now, I have a question... if I have the method add() in the parent, then I extend it and override the method add() in the child, how can I make the child add() do something and call the parent add()? they have the same name, so I know it's not this.add() (would get me into a loop)

SparK