In the first part, we covered the GameCycle subclass.
In this part, we cover the timer events.
Timers are just GameObject subclasses that implement TimerCallback to do the actual work of changing the cube z-rotation.
public class CubeAnimator extends GameObject implements TimerCallback { final String[] targets;
The constructor takes a list of names, not objects!
public CubeAnimator(String name, String[] targets) { super(name, true); this.targets = targets; }
The work happens in the execute method, which just loops through the name list, and attempts to locate each GO and adjust its properties.
Adjusting the transform is straightforward, and Properties.set must be called to trigger the notifications that will recompute the GOs transform matrix.
@Override public void execute(long delta, long elapsed, boolean last, Locator lc) { for (int ix = 0; ix < targets.length; ix++) { final GameObjectWithProperties go = lc.locate(targets[ix]); if (go == null) continue; final Transform rx = go.getAs(Constants.Property.TRANSFORM); if (rx != null) { rx.rz += 1f; // must re-set so property change notifications trigger go.set(Constants.Property.TRANSFORM, rx); } } }
The remaining methods are for configuration of the timer callbacks. This is set up as a continuous, repeating timer of 1000ms cycle. Since it is continuous, it is called every Timer Base Tick, which for this demo is 100ms (10 times per second).
If you need a constant rate, you must take the Timer Base Tick into account when calculating how much to change the model. For example, this demo rotates the cubes at 1 degree per tick, 10 degrees per second, but if we decrease the TBT to 10ms, the cubes would now rotate at 100 degrees per second.
@Override public void setConfig(TimerConfig tc, int tbt) { tc.durationMS = 1000; tc.continuous = true; tc.autoRepeat = true; } @Override public boolean getRegisterOnInstall() { return true; } }