-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackground.js
More file actions
70 lines (52 loc) · 1.54 KB
/
Background.js
File metadata and controls
70 lines (52 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ===========
// BACKGROUND
// ===========
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// A generic contructor which accepts an arbitrary descriptor object
function Background(descr) {
for (var property in descr) {
this[property] = descr[property];
}
// Default sprite, if not otherwise specified
this.sprite = this.sprite || g_sprites.background;
// Create reset values
this.rX = this.x;
};
// Initial, inheritable, default values
Background.prototype.x = 0;
Background.prototype.y = 0;
Background.prototype.velX = 0.25;
Background.prototype.scale = 1;
Background.prototype.halt = false;
Background.prototype.reset = function () {
this.x = this.rX;
this.y = this.rY;
this.scale = this.rS;
this.halt = false;
};
//update function for the background, move it from right to left.
Background.prototype.update = function (du) {
if (!this.halt) {
this.x -= this.velX * du;
}
if (this.x <= -this.sprite.width) {
this.x += this.sprite.width;
}
};
//function to draw the background
Background.prototype.render = function (ctx) {
var origScale = this.sprite.scale;
this.sprite.scale = this.scale;
this.sprite.drawAt(
ctx, this.x, this.y
);
this.sprite.drawAt(
ctx, this.x + this.sprite.width, this.y
);
this.sprite.scale = origScale;
};