Game Development Reference
In-Depth Information
The last thing you want when your player is clicking madly to shoot
at enemies is for the whole screen to suddenly turn blue because the
browser thinks the user is trying to select something. To avoid this,
you can either cancel the select event in JavaScript with document.
onselectstart = function() { return false; } or disable
it in CSS:
* {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Timing
As we start building more realistic examples, you'll notice delta parameters being
passed around to functions that affect physics. Those deltas represent an amount
of time since the last time physics was calculated, and they're used to smooth out
movement over time.
The naive way to move objects in code is to simply change the object's position. For
example, to move an object across the canvas, you might write obj.x += 10 inside
your animation loop to move it 10 units every frame. This approach suffers from the
issue that it is dependent on the frame rate. In other words, if your game is running
slowly (that is, fewer frames per second), your object will also appear to move
slowly, whereas if your game is running quickly (that is, more frames per second),
your object will appear to move quickly.
One solution is to multiply the speed by the amount of time that has passed
between rendering frames. For example, if you want your object to move 600 units
per second, you might write obj.x += 600 * delta . In this way, your object will
move a constant distance over time. However, at low frame rates and high speeds,
your object will be moving large distances every frame, which can cause it to do
strange things such as move through walls. At high frame rates, computing your
physics might take longer than the amount of time between frames, which will cause
your application to freeze and crash (this is called a spiral of death ). Additionally,
we would like to achieve perfect reproducibility. That is, every time we run the
application with the same input, we would like exactly the same output. If we have
variable frame deltas, our output will diverge the longer the program runs due to
accumulated rounding errors, even at normal frame rates.
 
Search WWH ::




Custom Search