Actionscript 3 :: Call A Superclass Method From A Subclass?
Aug 15, 2010
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.
I have a class called Animal that has a method foo() and I call it out within the Animal class.
Code: protected function foo():void { trace("Animal"); }
I also have a class called Cat that extends Animal and overrides the method foo()
Code: override protected function foo():void { trace("Cat"); }
However when calling the function foo() from the superclass it traces out Animal not Cat (using a Cat object).How can I make superclass call out methods in the subclass?
It's early days in my AS3 learning, and I'm looking for a little clarification regarding a variable typed to a superclass (in this case var mySpinner:MovieClip) which contains a reference to an new instance of a subclass (in this case Spinner.) I am trying to get my head around why my code compiles and runs and I am able to call the method rotater() on mySpinner even though mySpinner is typed as MovieClip, and the MovieClip class does not contain the method rotater().
On page 163 of the official Adobe Programming ActionScript 3.0 documentation it says the following, which I believe is related to my situation, but I am still in need of clarification: "Because each class defines a data type, the use of inheritance creates a special relationship between a base class and a class that extends it. A subclass is guaranteed to possess all the properties of its base class, which means that an instance of a subclass can always be substituted for an instance of the base class."
1: Is there a way to set superclass variables from within a subclass? I can't seem to accomplish it.
2: Also, why does it seem when I call a function, the superclass version is called instead of the sub? I construct the class using the sub, so whats the matter?
3: In one of the classes, I want to push a MovieClip it creates into an array, it works, but I get an undefined, then the value.
4: I have 3 levels of inheritance in my classes Specials -> Human -> Player, who do i call each one correctly from one another?
I have a Hero class that extends a Unit class that extends a SelectableObject class.[code]I have a movieclip in the library that links to the Hero. Inside the movieclip is another movieclip with the instance name "selector".In the Hero I can use selector.gotoAndStop (n) because the movieclip is linked to that class, but I can't do that for the SelectableObject because it's not linked to that.Is there a way to make selector.gotoAndStop (n) work for the SelectableObject? I can't even do var something:* = this.selector because the movieclip isn't linked to it.Or should I just remove the instance name for the selector in the movieclip so I can declare var selector:* = this.getChildAt (1) in the SelectableObject (the selector being the second child)?
For some time now I have been making a very easy game for iPhone in flash using as3. Recently I came in contact with a small problem, which is why I am posting this!
The problem: I have a superclass from which everything derives. In the superclass I initiate and place an Object on stage.
1. var myObject:typeA = new typeA();
2. stage.addChild(myObject);
As you can see this object follows the class 'typeA' which, ocf, has its own actionscript file. Inside of this file I have declared a global variable of type string.
What I want to do is change the varbiable on the new object from the superclass. Therefor I tried as following:
1. myObject.myVariable = 'someSortOfString';
Unfortunatly it didn't work and so I wonder how to do this; change a subclass' variable from the superclass.
I have an abstract superclass which has around 300 subclasses. I would like to log when certain methods on this parent are called, and I'd like to know the name of the subclass thats making the call. Is there any way to do this without rewriting all my subclasses to explicitly pass their names upwards to the parent's logging method? E.g., let's say I have a superclass Foo with a method writeLog(str). Foo has a subclass called FooBar, which calls super.writeLog("hello"). Can writelog just inherently figure out that a FooBar called it, without the need to rewrite everything so that we have to call super.writeLog("hello", "FooBar") ???
So, how would you create a class that can access a variable in the class where the subClass was created(superclass), I realise this might be unclear so in example
PHP Code: package {import flash.display.MovieClip;public class Main extends MovieClip {public va demoString:Strin = "TEST";public static var newClass:SubClass;public function
So, how would you create a class that can access a variable in the class where the subClass was created(superclass), I realise this might be unclear so in example
What passes to a subclass extending from a main or superclass, be it document or extending from a general MovieClip class etc? Does it pass absolutely everything from the superclass? That is to say, instance functions, variables defined inside these functions etc? Or just constructor variables? What passes through and does 'static' affect the transference of these values?
I've run into an extremely odd problem. I'll give a code example to start. This is the superclass Building.
Code: public var allowsEnemyMovement:Boolean = true; public var information:String = ""; public var buildingName:String ="";
[Code]...
Now here's the odd part. Only SOME of the variables defined in the superclass do this. the variable information, buildingName and health all will take the default value from the super class. But the variable defenseValue will take its value from Factory's constructor. What am I doing wrong here? I would like to keep default values in the superclass so that I don't end up with null pointer errors later just in case.
ActionScript Code: 1067: Implicit coercion of a value of type __AS3__.vec:Vector.ie.aro.floorplanviewer.model.buildingVOs:ConfigurationVO>[code]....
which surprises me as ConfigurationVO inherits from NodeVO.Is the compiler really not able to figure out that a Vector of a superclass should be able to hold a reference to a Vector of a subclass?
TypeError: Error #1009: Cannot access a property or method of a null object reference. the main idea is calling a method from subclass that inherit to a superclass.. here is the code
I'm trying to override a subclass's method but I think I need access to the superclass one higher up in the hierarchy. So I'd like to do something like super.super.methodName();
FYI, the actual problem I'm trying to solve should be explained by my code below:
public class A extends UIComponent{ override protected function keyDownHandler(event:KeyboardEvent):void{ super.keyDownHandler(event);
[Code]....
If I use the class A's KeyDownHandler method, you will see that remove() is called regardless. However, the docs state that I need to call UIComponent.keyDownHandler whenever I subclass UIComponent. Of course this works for the first subclass, but not that subclass's subclasses.
I realise that I could put the functionality all into A with something like if(this is B){...} but that seems to me to be a hack.
error When your accessing a method or property from a subclass. Here's what I have done so far but no luck in my script.
1. I tried waiting for my attached movieclip to be added to my stage using the setTimeout method but no luck.
var timedProcess:Number = setTimeout(initRevealDropDown, 1000); private function initRevealDropDown():void { clearTimeout(timedProcess); var myInterface:Interface = new Interface(); myInterface.revealDropDown(); }
2. I also tried extending my subclass to interface where my instance method is defined for revealing my dropdown menu, no luck on that one.
Can anybody tell me why my lb.init(x, y, h, w); call doesnt call! I get no errors yet my output panel only shows.[code]could it be an issue with the cast on lb?this class is for button which i am creating via the createLightButton()Method in another class the button is created init() in not called +NO ERRORS.Ive used an example for Moocks Essential ActionScript ch13 Avatar class to create this class and I can see no reason for it not to work.[code]
I'm tryng to make a public static method call another method, but Flash throws error 1180, sayng that the method called by the static method is undefined.
I need to get a javascript var in my Flash application. I like to be able to just set a variable in the javascript (client constraints) rather than define a function.
Can this be done? I am trying to use the ExternalInterface.call()
I have a class. I can call the constructor (initialize the class), and the constructor can then call a function/method inside it's own class (so I know it works), but when I try to call the same function/method myself from the instance of the class I just initialized, I get an error.
Can I access a static variable of a subClass' subClass? I don't want to instantiate it, just looking to get the variable because it has already been initialized by the subClass.example:
package { public class A extends MovieClip { private var classB:B = new B();
I have an issue with my eventListeners with the URLLoader, but this issue happens in IE, not in FF.
public function getUploadURL():void { var request:URLRequest = new URLRequest(); request.url = getPath();[code]....
The issue is that my getBaseURL gets executed automatically after I have executed the code at least once, but that is the case only in IE. What happens is I call my getUploadURL, I make sure the server sends an event that will result in an Event.COMPLETE, so the getBaseURL gets executed, and the listener is removed. If I call the getUploadURL method and put the wrong path, I do not get an Event.COMPLETE but some other event, and getBaseURL should not be executed.
That is the correct behavior in FireFox. In IE, it looks like the load() method does not actually call the server, it jumps directly to the getBaseURL() for the Event.COMPLETE. I checked the willTrigger() and hasEventListener() on _loader before assigning the new URLLoader, and it turns out the event has been well removed.
I simplified my code. To sum up quickly: in FireFox it works well, but in IE, the first call will work but the second call won't really call the .load() method; it seems it uses the previously stored result from the first call.
I need it in FlexUnit to test private methods. Is there any possibility to do this via reflection by using describeType or maybe flexUnit has some build in facility? I dislike artificial limitation that i cannot test private functions, it greatly reduces flexibility. Yes it is good design for me to test private functions, so please do not advise me to refactor my code. I do not want to break the encapsulation for the sake of unit testing.
Given an instance of MyClass, I want to be notified when someone calls an specific method.
example:
package bar; { public class MyClass { private var foo:int;
[code]...
So given a instance of MyClass ( var instance = new MyClass() ) i want to be notified every time getFoo is called. I have tried using reflection+annotations+dynamic classes, to find that in AS3 you can't change seal methods (methods defined in the class). I was wondering if i can subscript to a event in order to be notified when the method is called.
Well yeah, it sounds funny, but all I want is to read the value of Capabilities.version to get the right version of the Flash player? I have tried getting the version info from swfObject library, but it doesn't give the complete version info:[URL]..
FP version info have 4 numbers: major, minor, release and build. I couldn't find an API which can give me all four on all browsers without loading SWF. Hence I am looking for help here.
I've got a collection of custom objects with a method, Update(), in each of them. I need a way to iterate over the collection and call that method. This is easy to do in C# with a List, but I'm not sure how to do it in ActionScript.