Game Development Reference
In-Depth Information
maxTime = time;
}
}
6. The trigger method should call onTrigger , if it is enabled.
7. The onTrigger method should set time to 0 and set running to true .
8. The update method should first check whether the Timer is enabled and
running .
9. If it is, tpf should be added to the time.
10. Inside the same statement, we then create an iterator based on keySet of
timerEvents and parse through it. If the key (a float) is more than lastTime
and less or equal to the current time, we should get the corresponding value from
the timerEvents map and execute it. Otherwise, if the key is less than
lastTime , we should just continue using the following code:
Iterator<Float> it = timerEvents.keySet().iterator();
while(it.hasNext()){
float t = it.next();
if(t >lastTime&& t <= time){
TimerEvent event = timerEvents.get(t);
if(event != null){
event.call();
}
} else if(t <lastTime){
continue;
}
}
11. Outside of the previous loop, we check if time is more than maxTime , in which
case, we should set running to false .
12. Finally in the update method, we set lastTime to be equal to time .
With the basic logic done, let's take a look at how we can use the timer for something real
and use it to trigger an explosion by performing the following steps:
1. Copy the TestExplosion class from jMonkeyEngine's test package and strip
it from everything except the methods that create ParticleEmitters and the
lines in simpleInitApp , which uses them and sets up the camera.
Search WWH ::




Custom Search