I have a question I wanted to ask you guys, it’s pretty complex because it involved classes and interfaces and can get confusing. I’ll try my best to explain it below. The gist of the story is, when I click the duck, I want it to give birth and also increment the number of ducks in my farm. Here goes:
The Setup
There is the Abstract Supertype “Duck” class.
public class Duck {
var clickBehaviour:ClickBehaviour;
public function Duck( ) {
addEventListener(MouseEvent.CLICK, performClick);
}
public function performClick():void {
clickBehaviour.click();
}
}
The “Duck” class implements the “MallardDuck” concrete implementation.
public class MallardDuck extends Duck {
public function MallardDuck( ) {
clickBehaviour = new GiveBirth( );
}
}
Here is the “ClickBehaviour” interface.
public interface ClickBehaviour {
public function click( ):void;
}
And here is the behaviour implementation.
public class GiveBirth implements ClickBehaviour {
public function click( ):void {
//Increase the variable numberOfDucks in my DuckFarm class by 1
}
}
And finally, my DuckFarm.
public class DuckFarm {
var numberOfDucks:Number = 0;
public function DuckFarm( ) {
//My farm automatically has at least one duck
var duck:Duck = new MallardDuck( );
addChild(duck);
numberOfDucks++;
//numberOfDucks is now 1;
}
}
And that’s all the files.
The thing I figure is which is the best way to get the GiveBirth class to:
- Retrieve the value of numberOfDucks from the DuckFarm class.
- Increment it and send back the new value to the DuckFarm class.
- If I had a shootBehaviour, how could I delete/remove the duck I clicked on and decrement the value of numberOfDucks from the DuckFarm class.
Am I right to start exploring the Singleton route? That might help solve the first two issues, but not the third (the removing duck from stage part)…
Well, just thought I’d pitch this out there. Any advice is appreciated. Thanks!!!