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);
}
}
}
@Override
public boolean getAutorepeat() { return true; }
@Override
public boolean getContinuous() { return true; }
@Override
public int getDurationMS() { return 1000; }
@Override
public boolean getRegisterOnInstall() { return true; }
}