1 /** 2 A class to represent the player on the screen 3 @author <a href="mailto:matthewcasperson@gmail.com">Matthew Casperson</a> 4 @class 5 */ 6 function Player() 7 { 8 /** The speed that the player moves at 9 @type Number 10 */ 11 this.speed = 75; 12 /** True if the player is moving left, false otherwise 13 @type Boolean 14 */ 15 this.left = false; 16 /** True if the player is moving right, false otherwise 17 @type Boolean 18 */ 19 this.right = false; 20 /** True if the player is moving up, false otherwise 21 @type Boolean 22 */ 23 this.up = false; 24 /** True if the player is moving down, false otherwise 25 @type Boolean 26 */ 27 this.down = false; 28 29 /** 30 Initialises this object 31 */ 32 this.startupPlayer = function() 33 { 34 this.startupAnimatedGameObject(g_run_left, 300, 200, 1, 12, 20); 35 return this; 36 } 37 38 /** 39 Called when a key is pressed 40 @param event Event Object 41 */ 42 this.keyDown = function(event) 43 { 44 // left 45 if (event.keyCode == 37) 46 this.left = true; 47 // right 48 if (event.keyCode == 39) 49 this.right = true; 50 // up 51 if (event.keyCode == 38) 52 this.up = true; 53 // down 54 if (event.keyCode == 40) 55 this.down = true; 56 } 57 58 /** 59 Called when a key is pressed 60 @param event Event Object 61 */ 62 this.keyUp = function(event) 63 { 64 // left 65 if (event.keyCode == 37) 66 this.left = false; 67 // right 68 if (event.keyCode == 39) 69 this.right = false; 70 // up 71 if (event.keyCode == 38) 72 this.up = false; 73 // down 74 if (event.keyCode == 40) 75 this.down = false; 76 } 77 78 /** 79 Updates the object 80 @param dt The time since the last frame in seconds 81 @param context The drawing context 82 @param xScroll The global scrolling value of the x axis 83 @param yScroll The global scrolling value of the y axis 84 */ 85 this.update = function (/**Number*/ dt, /**CanvasRenderingContext2D*/context, /**Number*/ xScroll, /**Number*/ yScroll) 86 { 87 if (this.left) 88 this.x -= this.speed * dt; 89 if (this.right) 90 this.x += this.speed * dt; 91 if (this.up) 92 this.y -= this.speed * dt; 93 if (this.down) 94 this.y += this.speed * dt; 95 96 // keep the player bound on the screen 97 if (this.x > context.canvas.width - this.frameWidth) 98 this.x = context.canvas.width - this.frameWidth; 99 if (this.x < 0) 100 this.x = 0; 101 if (this.y > context.canvas.height - this.image.height) 102 this.y = context.canvas.height - this.image.height; 103 if (this.y < 0) 104 this.y = 0; 105 } 106 } 107 108 Player.prototype = new AnimatedGameObject;