A Flash Developer Resource Site

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    7

    Arrow How do you call a parent class'es method?

    '->title says it all

    how!!?!!?
    EDIT-or get a variable in a parent class(no i dont want to declare it with the child), i want to be able to on-demand call it, because it changes.

    as in, here is my main code
    _______________________________

    import MyClass;
    var WantThis:int = 7
    var myObject:MovieClip
    function getWantThis():int{
    return WantThis
    }

    myObject = new MyClass()
    addChild(myObject)

    _________________________
    now this is part of the code of "MyClass"
    _________________________

    package*bla bla bla...
    trace("the value of WantThis in my parent class is "+********************)

    _________________________


    '->what do i put here so that i can always have an updated value of the variable WantThis, on demand whenever i want to, because it would change!! do i access the variable? or call that method getWantThis??? how do i do either of those!!

    thanks for any help!!
    **i cant find anything on google :`(
    Last edited by AuxV3i; 12-17-2008 at 09:12 PM.

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    super

    To call a method, use: super.method();

    Same as a variable: super.var1;

    But you shouldn't need to use those unless you're overriding a method. If the method/variable is protected then you can call it like a normal method/variable. If it's private, you can't call it even with super. And if it's public, you can use it like a normal method/variable.

    super is only needed if you are overriding a method and need to still use the default behavior and add onto it.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm pretty sure that by "parent" he meant displayList parent, rather than superclass.

    The quick and dirty answer is that you can cast the parent to MovieClip to access dynamic properties:

    Code:
    var parentmc:MovieClip = parent as MovieClip;
    trace("the value of WantThis in my parent class is "+parentmc.getWantThis());
    The more involved answer is that you should not be reaching up the displayList to access things because that tightly couples the two classes (they no longer work independently).

    You could define an interface which declares getWantThis and have your main class implement it. Then you can have a method in MyClass which takes an instance of that interface and uses it.

    interface:
    Code:
    package {
      public interface WantThisProvider{
    
         function getWantThis():int;
    
      }
    }
    main:
    Code:
    package {
    
      public class Main extends MovieClip implements WantThisProvider{
        var WantThis:int = 7
        var myObject:MovieClip
    
        public function Main(){
          myObject = new MyClass();
          myObject.setWTProvider(this);
          addChild(myObject)
        }
    
        public function getWantThis():int{
          return WantThis;
        }
      }
    }
    MyClass:
    Code:
    package {
      public class MyClass extends MovieClip{
         private var wtprovider:WantThisProvider;
         
         public function MyClass(){
    
         }
    
         public function setWTProvider(wt:WantThisProvider):void{
           wtprovider = wt;
         }
    
         public function testWT():void{
           trace("the value of WantThis in my wtprovider is "+wtprovider.getWantThis());
         }
    
      }
    }

  4. #4
    Junior Member
    Join Date
    Dec 2008
    Posts
    7
    wow dang never thought it would be so complicated, but i'll try it thanks alot!

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's not complicated. The first 2 line code snippet I posted will work for your purposes. The problem is that your question is akin to asking "How do I harness my mules to my car to make it go?" You're asking the wrong question, because there's a much better way to do it.

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