A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: [RESOLVED] Extending Question (Inheritance issue)

  1. #1
    Senior Member
    Join Date
    Aug 2001
    Posts
    227

    [RESOLVED] Extending Question (Inheritance issue)

    Ok, i am hoping there is a way to do what i want with as3.

    In other languages, you have types of overriding and inheritance. If one class inherits from another, it inherits its methods (public & protected), variables, etc.

    AS3 does this well and i am grateful.. except for the one thing so far that i cannot get to work properly.

    If i have a Square class for example, which draws a square and takes constructor parameters in such as Width, Height, X, and Y, that all works fine.

    However if i do that, i cannot extend that class. For example, if i want a SquareButton class that takes a url string as a constructor parameter, but inherits from square, flash gives me errors because of Square having constructor values.

    It seems that if i make anything extend Square (extend =='s Inherit?), AS3 requires that the constructor parameters for anything extending Square have the same exact parameters... how can i work around this?


    ie, in C# i could do something similar, and the SquareButton class would simply add its URL:String parameter to that of the Square class, so then to create a SquareButton class you would have to give 5 parameters, SquareButton( Width, Height, X, Y, URL ).

    How can this be done in AS3? AS3 is implementing so much so nicely, it would seem that it would also use this constructor extending..

    Am i doing something wrong? Any help? It's getting insanely annoying not being able to do this and having to code around the constructor if i want to ever extend off that class.

  2. #2
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    Yeah, you can't overload methods/functions with AS3. Something you couldn't do with AS2 and I thought would be fixed.

    I'm sorry, but the only workaround I can think of is just have all the parameters, and then check

    Code:
    if (URL != undefined)
    {
    SetURL(URL); // Make a method in SquareButton that sets it.
    }
    else
    {
    // We know it's a Square and not a SquareButton
    }
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Zeusbwr,
    if you have such question, please post an example of the problem. It is much easier to look at it.
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Actually i am rather confused now...

    In many past attempts, i was unable to make any class extend another that took required parameters.

    That is, if B extends A, but to call A alone you need say A( param, param, param, param ) (and thus showing it requires 4 parameters), i would be unable to make B Extend A because B was expecting those same parameters, or so it seemed.

    *confused*

    I'll be toying with this.. feel free to give any insight. Early on i was unable to do it so i've been specifically making classes around this problem, basically if i wanted the possibility to ever extend the class, i would make the class take no parameters in the constructor.

    Maybe there was an error message that mislead me on its true meaning...

  5. #5
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    When you extend a class, the subclass calls the superclass's constructor in its own constructor to initiate properties and perform other operations that are needed there. You can specify this call using super().
    code:
     // pseudo
    class SuperClass {
    function SuperClass(num){ this.num = num; }
    }
    class SubClass extends SuperClass {
    function SuperClass(){
    super(5)
    }
    }


    If you do not add a call to super() yourself, Flash does it automatically, but does so using a call to super() with no arguments. Since ActionScript 3 requires all arguments be passed unless a default value is provided, not explicitly calling super with the necessary arguments passed will result in an error. So what you need to do is simply call super as needed (or provide default values for the superclass).
    code:
    package {
    class Square {
    Square( Width, Height, X, Y ) {
    this.drawSquare(Width, Height, X, Y );
    }
    }
    }
    package {
    class SquareButton extends Square {
    SquareButton( Width, Height, X, Y, URL ) {
    super(Width, Height, X, Y);
    this.URL = URL;
    }
    }
    }


  6. #6
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    awesome! Thanks a ton guys! That partially was my problem i am sure. the super keyword will help me a ton!

    Any other semi hidden keywords that i should be aware of? hell super() alone opens up a bunch of possibilities!

  7. #7
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    http://www.adobe.com/go/AS3LR/

    Everything you need is there. Check out the language elements for things like super.

  8. #8
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    oh i use the langref constantly, but randomly finding the thing you need is pretty hard

    I didn't know my problem exactly, so i couldn't really look for the solution.


    But yea, without the AS3 & Flex2 lang ref it would be insane to do this for me lol.


    Thanks again

  9. #9
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    It can be tricky if you don't even know what you're missing

    I guess then you'll probably have to rely on books and tutorials to get you to a point where you know at least how things work, then worry about figuring it all out when you start to use it (or try to)

  10. #10
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Well so far i "think" i've got a pretty good grasp. I do come from other languages, and so far AS3 is living up to my expectations. so far the only thing i truely do miss is function overriding.

    I think the only huge thing i was missing was the super() keyword. Oh and i do need to do some research on garbage collection, as that can quickly become out of control with any language lol... with improper use that is.

  11. #11
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Do you mean overloading? You can override functions in Flash; thats not a problem.

    As for garbage collection, read up on:
    http://www.gskinner.com/blog/archive...source_ma.html

  12. #12
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    erm yea overloading, sorry.

    ie
    Code:
    // pseudo
    
    public function test( one:int ):void
    { // Do stuff
    }
    
    public function test( one:int, two:int ):void
    { // Do different stuff
    }
    
    public function testb( one:int ):void
    {
      this.testb( one, 0 );
    }
    public function testb( one:int, two:int ):void
    { //Do more stuff
    }

    Overloading

    Overriding is a must for inheritance, overloading is .. well a must imo, but most convenience for me.

  13. #13
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    haha, as if just by chance i decided to check the ram usage over time of my little gallery app.

    I realized i was doing the switch improperly but i didn't really care, i'd fix it later. Welp, spose now is the time, with 3 images (medium sized), loading ever 5sec or so in a slideshow fashion, you end up having 50megs of ram used in no time. It basically stacks 1.5meg every image lol.


    Time to enable proper management

  14. #14
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    Quote Originally Posted by Zeusbwr
    erm yea overloading, sorry.

    ie
    Code:
    // pseudo
    
    public function test( one:int ):void
    { // Do stuff
    }
    
    public function test( one:int, two:int ):void
    { // Do different stuff
    }
    
    public function testb( one:int ):void
    {
      this.testb( one, 0 );
    }
    public function testb( one:int, two:int ):void
    { //Do more stuff
    }

    Overloading

    Overriding is a must for inheritance, overloading is .. well a must imo, but most convenience for me.

    Wow,
    That totally wouldn't work in AS2

    -Danny
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  15. #15
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    In fact, I did some googling on overloading. More articles keep saying AS 3 doesn't support it... Was there an update somewhere?

    -Danny
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  16. #16
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    ? I'm confused on whats different? AS3 never supported Overloading, nor did AS2. So the articles are right.. (least to my knowledge)

  17. #17
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    As far as I know you could never have two functions with the same name as stated above (function test) in one class. You always get an error whether AS2 or AS3.
    - The right of the People to create Flash movies shall not be infringed. -

  18. #18
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Quote Originally Posted by KigD
    In fact, I did some googling on overloading. More articles keep saying AS 3 doesn't support it... Was there an update somewhere?

    -Danny
    Thats what we've been saying that overloading is *not* supported and wishing that it was, not that it is now.

  19. #19
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    I know, sorry. I thought you were saying it WAS allowed.

    -Danny
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  20. #20
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    No problems

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center