I have an OOP related problem with Flash, actionscript 3. It's a personal project, and I am looking for a design pattern or workaround for this problem, and my goal is to learn new things.
I have created a class called Chain. I created this util-class to make delayed function calling easy. You can make a chain of functions, by adding them with a delay in milliseconds. This chain can be executed multiple times, even in reversed order. This class has functions which returns itself. That makes it possible to have a jQuery styled syntax like this:
var chain:Chain = new Chain();
chain.wait(100).add(myFunction1,300).wait(300).add(myFunction2,100);
// etc..
For the example I have left lots of functions just to demonstrate the problem. The Chain class is mostly pure for adding functions and start/stopping the chain.
public class Chain
{
function wait(delay:int = 0):Chain
{
// do stuff
return this;
}
public function add(func:Function, delay:Number = 0):Chain
{
list.push( new ChainItem(func, delay) );
return this;
}
}
Now, I have a another class called ChainTween. I am trying to split things up to keep the Chain with some core functions and have ChainTween do some animating tricks. I had the idea to create a little tweenengine based on the Chain class. Currently it extends Chain. It uses lots of protected variables from the Chain class and overrides also some core functions for Chain to add the tween functions inside the process of Chain.
public class ChainTween extends Chain
{
function animate(properties:Object = null, duration:Number = 0, easing:Function = null):ChainTween
{
// do stuff
return this;
}
}
Now this is the problem: I want to keep the chaining syntax, but wait() returns a Chain instance and Chain has no animate function.
var chain:ChainTween = new ChainTween();
chain.wait(100).animate({x:200}, 100).wait(250);
I have tried to override the wait() and add() function in the ChainTween class but this causes an incompatible override.
I could cast chain.wait(100) as ChainTween, but this is very ugly and not useful when I am chaining lots of them. Now I don't want to add any of the ChainTween functions to Chain (no dummy functions too), and I want to keep completion to all functions, so returning Object is not an option too. I tried to use an interface, but this gives the same problem, since the functions of an interface should be implemented in the class that implements it.
Now I have thought about creating an instance of Chain inside ChainTween, but this does not allow me to override functions, and then I should make lots of properties public instead of protected, which is not preferred too.
Is this possible and does anyone has a great solution for this?