So I was watching someone else program in actionscript. And I have never really had a desire or great use to use function pointers( pass a method as an argument) and they were using an array to pass special effects to some graphic/text things, the special effects were usually simple 2-5 line function/methods and it just seemed so easy, without my typical method of having a ton of boolean statements everywhere.
However after seeing them do some things quite conveniently, I was hoping to find an alternative.
In my case I just wanted to be able to do it to handle certain ‘effects’ that I can add/remove from an update/scripts list
Here is just a random limited example.
logo
scripts.add(new fade());
}
ArrayList<functionPointer> scripts = new ArrayList<functionPointer>();
public float alpha;
public class fadeOut implements functionPointer{
@Override
public void execute() {
alpha=-.01f;
}
}
public class fade implements functionPointer{
@Override
public void execute() {
alpha+=.01f;
if(alpha >= 1){
scripts.remove(this);
scripts.add(new fadeOut());
}
}
}
public void someUpdateFunction(){
for(functionPointer fp : scripts){
fp.execute();
}
}
Then of course
functionPointer.java
public interface functionPointer {
public void execute();
}
Would there be a better way of doing this?
Essentially whats a good way to embed possibly sequential and/or concurrent methods to be utilized by the same class in several areas that need to come and go.
Alternatively, I could have a lot of embedded if/else and boolean statements and other conditionals all over the place.
Maybe there is something out there that would work a lot better that I just don’t know about? (Some sort of event/script/callback manager) thingy?