-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.js
39 lines (34 loc) · 1.06 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
import {spriteLoaded} from './Assets.js';
let ctx = document.getElementById('gameCanvas').getContext('2d');
// The object class to handle sprites, which area a extension of images.
class Sprite {
// Create a spirte based of Assets.json.
constructor(spriteName, imgSrc, frameCount) {
var self = this;
this.sprite = spriteName;
this.image = new Image();
this.image.onload = function() {
self.width = this.width / frameCount;
self.height = this.height;
self.halfWidth = self.width/2;
self.halfHeight = self.height/2;
spriteLoaded();
console.log("Sprite:" + spriteName + ' Loaded');
}
this.image.src = imgSrc;
}
// Draw a image at a frame.
draw(dx, dy, frame) {
ctx.drawImage(
this.image,
this.width * frame,
0,
this.width,
this.height,
dx-this.halfWidth,
dy-this.halfHeight,
this.width,
this.height);
}
}
export default Sprite;