-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.js
58 lines (50 loc) · 1.24 KB
/
sprite.js
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
function sprite(context, width, height, image, numberOfFrames, loop) {
var that = {};
that.context = context;
that.width = width;
that.height = height;
that.source = image.src;
that.image = image;
that.loop = loop;
that.flipped = false;
var frameIndex = 0;
var tickCount = 0;
var ticksPerFrame = 6;
var numberOfFrames = numberOfFrames || 1;
that.render = function(x_position, y_position) {
that.context.drawImage(
that.image,
frameIndex * that.width / numberOfFrames,
0,
that.width / numberOfFrames,
that.height,
x_position,
y_position,
that.width / numberOfFrames,
that.height);
};
that.update = function() {
tickCount += 1;
if (tickCount > ticksPerFrame) {
tickCount = 0;
if (frameIndex < numberOfFrames - 1) {
frameIndex += 1;
}
else if (that.loop) {
frameIndex = 0;
}
}
};
that.flip = function(dir){
//Switch image from source.png to source_flip.png and back
if (dir === 'left' || (dir != 'right' && this.flipped)) {
this.image.src = this.source;
this.flipped = false;
} else if (dir === 'right' || dir != 'left') {
var index = this.source.indexOf('.png');
this.image.src = this.source.slice(0, index) + '_flip.png';
this.flipped = true;
}
}
return that;
}