HTML and CSS Reference
In-Depth Information
this.left = true;
if (key.keyCode == 39 || key.keyCode == 68)
this.right = true;
}
this.keyUp = function(key) {
if (key.keyCode == 38 || key.keyCode == 87)
this.up = false;
if (key.keyCode == 40 || key.keyCode == 83)
this.down = false;
if (key.keyCode == 37 || key.keyCode == 65)
this.left = false;
if (key.keyCode == 39 || key.keyCode == 68)
this.right = false;
}
4.
Each of these functions performs a number of checks to determine whether a key
has been pressed or released. As a result these checks determine in which direction
the player should move if a certain key has been pressed.
5.
In order to move the player in the direction in question, we must update the number
of units the player has moved each frame by introducing an Update function to the
player object as follows:
this.Update = function (deltaTime, context, deltaX, deltaY) {
if(this.up)
this.y -= this.velocity * deltaTime;
if(this.down)
this.y += this.velocity * deltaTime;
if (this.left)
this.x -= this.velocity * deltaTime;
if (this.right)
this.x += this.velocity * deltaTime;
}
 
Search WWH ::




Custom Search