Game Development Reference
In-Depth Information
private float floatCycleTimeLeft;
private boolean floatingDownwards;
private Vector2 floatTargetPosition;
Next, make the following modifications to the same class:
private void init () {
dimension.set(1, 1.5f);
regEdge = Assets.instance.rock.edge;
regMiddle = Assets.instance.rock.middle;
// Start length of this rock
setLength(1);
floatingDownwards = false;
floatCycleTimeLeft = MathUtils.random(0,
FLOAT_CYCLE_TIME / 2);
floatTargetPosition = null;
}
These changes make sure that the floating mechanism is correctly initialized. The
starting value for the float direction is set to up ; the cycle time is randomly picked
between 0 and half of the maximum float cycle time. Using a random cycle time
gives the floating effect a more natural look because every rock seems to move just
on its own. The floatTargetPosition variable is used to store the next target
position, as shown here:
@Override
public void update (float deltaTime) {
super.update(deltaTime);
floatCycleTimeLeft -= deltaTime;
if (floatTargetPosition == null)
floatTargetPosition = new Vector2(position);
if (floatCycleTimeLeft<= 0) {
floatCycleTimeLeft = FLOAT_CYCLE_TIME;
floatingDownwards = !floatingDownwards;
floatTargetPosition.y += FLOAT_AMPLITUDE
* (floatingDownwards ? -1 : 1);
}
position.lerp(floatTargetPosition, deltaTime);
}
 
Search WWH ::




Custom Search