Game Development Reference
In-Depth Information
In the handleInput method, you first check whether the selector is visible. If not, it doesn't need to
handle input:
if (!this.visible)
return;
You then check whether one of the arrows was pressed. If so, you calculate the desired animal velocity:
var animalVelocity = Vector2.zero;
if (this._arrowdown.pressed)
animalVelocity.y = 1;
else if (this._arrowup.pressed)
animalVelocity.y = -1;
else if (this._arrowleft.pressed)
animalVelocity.x = -1;
else if (this._arrowright.pressed)
animalVelocity.x = 1;
animalVelocity.multiplyWith(300);
If the player clicked the left mouse button or touched the screen (it doesn't matter where), you set
the state of the animal selector to invisible again:
if (Mouse.left.pressed || Touch.containsTouchPress(Game.screenRect))
this.visible = false;
Finally, if the velocity you calculated isn't zero, and there is a target penguin, you update its velocity:
if (this.selectedAnimal !== null && animalVelocity.isZero)
this.selectedAnimal.velocity = animalVelocity;
In the handleInput method in the Animal class, you have to handle clicking an animal. However,
there are some situations in which you don't have to handle this:
The animal is in a hole in the ice.
The animal isn't visible.
The animal is already moving.
In all these cases, you don't do anything, and you return from the method:
if (!this.visible || this.boxed || !this.velocity.isZero)
return;
If the player didn't touch or click the animal, you can also return from the method. Therefore, you
add the following if instruction that verifies this:
if (Touch.isTouchDevice) {
if (!Touch.containsTouchPress(this.boundingBox))
return;
} else {
if (!Mouse.left.pressed || !this.boundingBox.contains(Mouse.position))
return;
}
 
Search WWH ::




Custom Search