user893649
user893649

Reputation:

Need to 'extend' various classes in ActionScript for one class

I have a class that needs to use the graphics methods of the Sprite class, so I've extended said class using Sprite. However I have my own custom class that the first class ALSO needs to extend from to use the properties and methods written within.

Of course I could extend my custom class by Sprite and the first class would be able to use both, but my custom class has nothing to do with Sprite and shouldn't extend it.

How would I go about using the properties and methods of my custom superclass AND use the graphics properties of the Sprite class in one subclass?


My simple example:

My custom class is named MovingObject.as and contains two variables and some methods that need to be inherited by a subclass. The purpose of MovingObject is to hold information relating to the speed and acceleration of their child classes.

The child class 'RaceCar' is drawn using the graphics methods given by expanding Sprite, but I need to expand from MovingObject allowing the RaceCar to inherit speed and acceleration.

I'm stuck on how to both give RaceCar the features of expanding from Sprite and my MovingObject class.

Upvotes: 1

Views: 293

Answers (2)

net.uk.sweet
net.uk.sweet

Reputation: 12431

If your first class needs to be added to the display list it should almost certainly extend Sprite. How you make the functionality of your second custom class available to it will probably depend on what that functionality is (if you could give a brief description it would make answering the question much easier).

However, in addition to extension (which you've ruled out), I can think of two other methods:

  1. Give your first class an instance of your custom class to act on either through a setter or by instantiating the custom class directly in your first class. This is known as composition.
  2. Make the methods on your custom class static so that they can be called by your first class without instantiating it. This is a method typically used for utility type classes.

Update:

I think you might be complicating things unnecessarily in this case, unless you envisage having sub-classes of MovingObject which are not visually represented on screen (which seems unlikely to me). If not, it's perfectly valid for MovingObject to extend Sprite, in effect augmenting it with moving functionality, and this forming the base class for your RaceCar (and any other moving, visible objects).

Upvotes: 0

Torious
Torious

Reputation: 3424

Multiple inheritance doesn't exist in AS3, but one decent pattern to approach this problem is the following:

  • You have classes Base and Extension, where Extension must extend both Base and Sprite.
  • Make Base an interface, and your current Base class becomes a default implementation of that interface, say BaseImpl.
  • Make Extension extend Sprite and implement Base.
  • The implementation of Base will be done by aggregating a BaseImpl instance, which means it will delegate all Base methods to a contained instance of BaseImpl.

Example:

interface Base {
    function hello():String;
}

class BaseImpl implements Base {
    public function hello():String {
        return "Hello";
    }
}

class Extension extends Sprite implements Base {

    private var base:BaseImpl = new BaseImpl();

    public function hello():String {
        return base.hello();
    }
}

In this way, Extension would delegate all functionality of Base to the BaseImpl instance it contains.

Btw, in the Flash API itself, there is an example of exactly this pattern: EventDispatcher is a default implementation of IEventDispatcher, so that other classes can extend a class other than EventDispatcher and still implement IEventDispatcher through aggregation.

Good luck!

Upvotes: 0

Related Questions