I'm having an issue using a class I've created as the base class for library symbols:
I've created a class AvSkin
which will act as the display for an instance of AvChild
. It looks like this:
package avian.environment.skins
{
import flash.display.DisplayObject;
/**
* @author Marty Wallace
* @version 1.0.0
*/
public class AvSkin extends DisplayObject
{
/**
* Getters
*/
public function get top():Number{ return y - height/2; }
public function get left():Number{ return x - width/2; }
public function get bottom():Number{ return y + height/2; }
public function get right():Number{ return x + width/2; }
}
}
Obviously doesn't do a lot, but the point is that I can add to it later on (ie a render()
method).
Problem is, because this extends DisplayObject
(so that I can make the skin a TextField
, SimpleButton
, Shape
, etc) and not MovieClip
, it throws this error if I set it as the base class for a library symbol:
5000: The class 'avian.environment.skins.AvSkin' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Is there a way around this? I don't want to do either of the following:
- Make
AvSkin
extendMovieClip
. - Create a class for my library sy开发者_JS百科mbol that extends
AvSkin
.
See here for a detailed representation of what extends DisplayObject, which might help back the reasoning behind my question.
Library symbols have a given type, i.e. MovieClip
or Sprite
and so on. If you want to inherit from these, you have to use this as base class. There is no workaround for this, it's an OOP paradigm for languages which do not support multiple inheritance, which AS3 is. You can't extend something in the middle of an inheritance tree.
For your task you could have an Interface
. But you have to implement this interface in all your derived classes. But the implementation logic could be out sourced in a static class to ensure maintainability.
interface IAvSkin
{
function get top():Number;
function get left():Number;
function get bottom():Number;
function get right():Number;
}
public class AvSkin
{
public static function getTop(obj:DisplayObject):Number
{
return obj.y - obj.height/2;
}
// and so on
}
public class AvSkinMovieClip extends MovieClip implements IAvSkin
{
public function get top():Number{ return AvSkin.getTop(this); }
// and so on
}
public class AvSkinMovieClip extends Sprite implements IAvSkin
{
public function get top():Number{ return AvSkin.getTop(this); }
// and so on
}
How about extending Sprite? That is one of the most barebones DisplayObject subclass that won't have most of the MovieClip functionalities.
How about just using composition? You can instantiate the AvSkin and send the class you want to extend into it:
package avian.environment.skins
{
import flash.display.DisplayObject;
/**
* @author Marty Wallace
* @version 1.0.0
*/
public class AvSkin extends DisplayObject
{
protected var _component:DisplayObject;
public function AvSkin(component:DisplayObject) {
addChild(_component = component);
}
/**
* Getters
*/
public function get top():Number{ return y - height/2; }
public function get left():Number{ return x - width/2; }
public function get bottom():Number{ return y + height/2; }
public function get right():Number{ return x + width/2; }
}
}
and use it like this:
var skin:AvSkin = new AvSkin(new MyMovieClip());
Now you can superclass it as much as you'd like.
精彩评论