-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.js
40 lines (37 loc) · 1.22 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
var sprite = function(options){
//options you must send are ticks per frame, number of frames, context, width, height, image
this.frameindex = 0;
this.tickCount = 0;
this.ticksPerFrame= options.ticksPerFrame; // how man ticks before changing frame
this.numberOffFrames = options.numberOfFrames; // how many frames/images in the sprite doc
this.context = options.context; //where to to draw the sprite--> pass the main game board
this.width = options.width; // width of the ENTIRE sprite page
this.height = options.height; //height of the ENTIRE sprite page
this.image = options.image; //the actual image of the sprite
this.yaxis = options.yaxis;
this.xaxis= options.xaxis;
this.update = function(){
this.tickCount +=1;
if (this.tickCount > this.ticksPerFrame){
this.tickCount = 0;
if (this.frameindex < this.numberOffFrames - 1){
this.frameindex +=1;
}else{
this.frameindex = 0;
}
}
};
this.draw = function(){
this.context.drawImage(
this.image,
this.frameindex * this.width / this.numberOffFrames,
0,
this.width /this.numberOffFrames,
this.height,
this.xaxis,
this.yaxis,
this.width / this.numberOffFrames,
this.height
);
}
};