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
21        /**
22            Initialises this object
23        */
24        this.startupPlayer = function()
25        {
26            this.startupAnimatedGameObject(g_idle_left, 300, 350, 4, 6, 20);
27            return this;
28        }
29
30        /**
31            Called when a key is pressed
32            @param event Event Object
33        */
34        this.keyDown = function(event)
35        {
36            var updateRequired = false;
37
38            // left
39            if (event.keyCode == 37 && !this.left)
40            {
41                this.left = true;
42                updateRequired = true;
43            }
44            // right
45            if (event.keyCode == 39 && !this.right)
46            {
47                this.right = true;
48                updateRequired = true;
49            }
50
51            if (updateRequired)
52                this.updateAnimation();
53
54        }
55
56        /**
57            Called when a key is pressed
58            @param event Event Object
59        */
60        this.keyUp = function(event)
61        {
62            // left
63            if (event.keyCode == 37)
64            {
65                this.left = false;
66                this.setAnimation(g_idle_left, 6, 20);
67            }
68            // right
69            if (event.keyCode == 39)
70            {
71                this.right = false;
72                this.setAnimation(g_idle_right, 6, 20);
73            }
74
75            this.updateAnimation();
76        }
77
78        /**
79            Updates the current animation depending on the movement
80            of the player. This accounts for the fact that both
81            the left and right arrow keys can be pressed at the
82            same time.
83        */
84        this.updateAnimation = function()
85        {
86           if (this.right && this.left)
87                this.setAnimation(g_idle_left, 6, 20);
88            else if (this.right)
89                this.setAnimation(g_run_right, 12, 20);
90            else if (this.left)
91                this.setAnimation(g_run_left, 12, 20);
92        }
93
94        /**
95            Updates the object
96            @param dt The time since the last frame in seconds
97            @param context The drawing context
98            @param xScroll The global scrolling value of the x axis
99            @param yScroll The global scrolling value of the y axis
100       */
101      this.update = function (/**Number*/ dt, /**CanvasRenderingContext2D*/context, /**Number*/ xScroll, /**Number*/ yScroll)
102       {
103           if (this.left)
104               this.x -= this.speed * dt;
105           if (this.right)
106               this.x += this.speed * dt;
107
108           // modify the xScroll value to keep the player on the screen
109           if (this.x > context.canvas.width - this.frameWidth + xScroll)
110               g_GameObjectManager.xScroll = this.x - (context.canvas.width - this.frameWidth);
111           if (this.x < xScroll)
112               g_GameObjectManager.xScroll = this.x;
113       }
114   }
115
116   Player.prototype = new AnimatedGameObject;

Top