Game Development Reference
In-Depth Information
Because the ball is now moving, you need to give it a velocity . This velocity is a vector in the
direction of the place where the player clicked. You can calculate this direction by subtracting the
ball position from the mouse position. Because the velocity has an x component and a y component,
you need to do this for both dimensions:
ball.velocity.x = (Mouse.position.x - ball.position.x);
ball.velocity.y = (Mouse.position.y - ball.position.y);
Calculating the velocity in this way also gives the desired effect that when the user clicks further
from the cannon, the velocity is greater, because then the difference between the mouse position
and the ball position is also greater. However, if you were to play the game now, the ball would move
a bit slowly. Therefore, you multiply this velocity with a constant value that gives the ball a velocity
that is usable in the context of this game:
ball.velocity.x = (Mouse.position.x - ball.position.x) * 1.2;
ball.velocity.y = (Mouse.position.y - ball.position.y) * 1.2;
I chose the constant value of 1.2 after testing the gameplay with different values. Each game will
have a number of these gameplay parameters that you'll need to tweak while play-testing the
game to determine their optimal value. Finding the right values for these parameters is crucial for a
balanced game that plays well, and you need to make sure the values you choose don't make the
game overly easy or difficult. For example, if you chose a constant value of 0.3 instead of 1.2, the
ball would move much more slowly. This would make the game much more difficult, and it might
even make the game unplayable because the ball might never be able to reach the furthest can.
If you add the handleInput method to ball , it isn't automatically called. You need to do that explicitly
in the painterGameWorld object. Therefore, you add an extra instruction to the handleInput method
of that object:
painterGameWorld.handleInput = function () {
ball.handleInput();
cannon.handleInput();
};
Updating the Ball
A big advantage of grouping related variables and methods together in objects is that you can keep
each object relatively small and clear. You can design objects that more or less reflect the various
kinds of game objects in the game. In this case, you have an object for the cannon as well as for the
ball. The goal is that each of these game objects deals with player input relevant for that object. You
also want the game objects to update and draw themselves. That is the reason you added an update
method and a draw method to ball , so you can call these methods in the game loop methods of
painterGameWorld .
 
Search WWH ::




Custom Search