Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 ViewsA summary of my current situation:
I am programming a game with various types of enemies that are fairly similar for the most part (most move in the same pattern, some shoot, some have a special shoot attack etc.) so to keep things organized I am creating a small structure of classes that build on each other.
For example I have a base move-enemy class and then a shoot-enemy class that extends the move-enemy class.
Right now the various levels of classes mainly build on each other by overwriting functions, calling the super function, and then adding it's own code. The base classes also have some empty functions which are just there to get overwritten.
Now my real question is:
Would it be more efficient for me to have the base classes dispatch events to get picked up by sub-classes, or to have the base classes call functions which then get overwritten by sub-classes?
Having events sent would make things a bit more organized (In my opinion) because then you can actually understand whats going on from a sub-class without looking too much at what the base-classes are doing.
But also I don't want to slow the game down just because I thought this way would be easier to read.
I hope I'm not burying my real question here by being too wordy : /.
It's basically:
BaseClass {
private function SomeFunction{
SayHi();
}
protected function SayHi(){
//Empty function
}
}
SubClass extends BaseClass {
override protected function SayHi(){
trace("Hello");
}
}
As opposed to:
BaseClass {
private function SomeFunction{
var e:CustomEvent = new CustomEvent("SAY_HI");
dispatchEvent(e);
}
}
SubClass extends BaseClass {
public function SubClass(){
addEventListener(CustomEvent.SAY_HI,greeting);
}
private function greeting(e:CustomEvent){
trace("Hello");
}
}
Where the main advantage with event system is that you can change how the BaseClass sends the event without having to adjust the SubClass
You forgot to use code tags, didn't you?
Having things run from the base class is theoretically more efficient.
Flash Player works by periodically running a "sweeper". Anything that is weak-referenced, old variables, marked event handlers, .etc get sweeped (or removed from memory) once they are no longer required. Running functions, variables, .etc from subclasses force the sweeper to scan both the subclass and superclass.
Once you finish with your subclass, the sweeper sweeps it, then sweeps the superclass. However, chances are the sweeper is constantly going to be in your superclass as you'll be using it for other subclasses, but if you can avoid using the subclass - or limit using it, then the sweep will not have to spend as much time in there.
Practically speaking, unless you're developing for very minimal resource systems, like phones, getting into the gritty detail like this isn't going to matter. Computers you're developing for aren't even going to blink an eye at the difference between your sub/superclasses. Actionscript floating point integers leak more memory that'll clog your resources that class sweeping and reading.
A note on the empty function in your superclass though, it's interesting you included it. Why? You don't need EVERY function in the subclasses to be listed in the superclass, you just need every function in the superclass to be listed in the subclass.