-
Inheritance problem with movieclip subclasses
I want to create a movieclip class that subclasses another movieclip class and inherits all its properties (rather than having to rely on the include statement). In my project I'm using:
An abstract parent class that extends a movieClip: "infantryDroid"
Code:
//
package{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class infantryDroid extends MovieClip{
public var cellW:int=40
public var speed:int;
public var _root:MovieClip;
//...Other methods and variables go here
}
}
A class that inherits all methods and properties of the parent movieclip:
"droid major"
Code:
//Child class
package{
public class droidMajor extends infantryDroid{
//Variables specific to this subclass go here
public function droidMajor(){
speed=4;
addEventListener(Event.ADDED,instance_create);
}
public function instance_create(event:Event):void{
//Initialization of variables when object is added to stage.
//refference to main timeline
_root=MovieClip(root)
...
removeEventListener(Event.ADDED,instance_create)
}
public function move(){
...
switch(dir){
case 0:
y=y-speed;
}
...
}
//methods specific to this subclass go here
}
}
However everytime I try this, flash acts as though nothing was imported from the parent movieclip class.
If I was to try the above code I would get the message that _root and speed (which were defined in the parent class) "are undefined".
What Am I doing wrong?
-
With minor exceptions, that looks all right. I assume you've cut down droidMajor for brevity. But if it does not import stuff like Event, I'd think you'd get a compile error.
By convention, class names should begin with a capital letter. DroidMajor and InfantryDroid.
How are you instantiating your droidMajor? And what is the exact error message you are getting?
-
In the constructor of your InfantryDroid class, put this:
super();
That will make your class extendable.
Also you will need to import the parent class in the child class.
-
A super() call should not technically be necessary, unless the superclass constructor requires arguments. It's implicit. But it is good practice to include one anyway.
Also, since droidMajor and infantryDroid are in the same package, he should not need to import infantryDroid into droidMajor. He will need to import Event and MovieClip and other stuff that is used.
-
Originally Posted by 5TonsOfFlax
A super() call should not technically be necessary, unless the superclass constructor requires arguments. It's implicit. But it is good practice to include one anyway.
Also, since droidMajor and infantryDroid are in the same package, he should not need to import infantryDroid into droidMajor. He will need to import Event and MovieClip and other stuff that is used.
Dammit 5! Hahaha
I had always wondered about the 'super()' call and read on it 2 days ago. Just when I thought I understood, you have to come in and screw it all up for me again... lol
-
Re: Inheritance problem with movieClip subclasses
Originally Posted by 5TonsOfFlax
Also, since droidMajor and infantryDroid are in the same package, he should not need to import infantryDroid into droidMajor. He will need to import Event and MovieClip and other stuff that is used.
Actually They're in different packages. The reason is that I have other classes that I want to extend infantryDroid:
- sprinter droid
- Droid minor
"An actionscript file can only contain one externally visible definition"
If you know how to associate multiple actionscript files with the same package please let me know
Originally Posted by 5TonsOfFlax
How are you instantiating your droidMajor? And what is the exact error message
you are getting?
I get the error message "access of undefined property speed" (along with any other public variables imported from the infantryDroid class).
As for instanciation, it gets a bit complicated as I've implemented my own method for object pooling. I usually make a call to the function:
Code:
generateInstanceCR(ObjType,x1,y1,dir=null)
Which:
- Creates an object of ObjType (or redeems it from an object pool)
- Sets its coordinates to x=x1; y=y1
- Resets variables such as health and target, if redeemed from a pool
- mainSprite.addChild(iObj) //Where mainSprite, is the game container sprite.
P.s. my game is at:
http://glider521al.110mb.com/Gamemak...roidGrids.html
-
All you gotta do for files to be in the same package is to put them all in the same directory.
I think an interface might help you here. It will let you share methods and variables between different classes.
Look at this link, he explains it pretty simply:
http://www.zedia.net/2008/how-to-use...ctionscript-3/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|