Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sprite {
- constructor(options) {
- this.image = options.image;
- this.frameIndex = 0;
- this.tickCount = 0;
- this.ticksPerFrame = options.ticksPerFrame || 0;
- this.numberOfFrames = options.numberOfFrames || 1;
- this.width = options.width;
- this.height = options.height;
- }
- update() {
- this.tickCount++;
- if (this.tickCount > this.ticksPerFrame) {
- this.tickCount = 0;
- if (this.frameIndex < this.numberOfFrames - 1) {
- this.frameIndex++;
- } else {
- this.frameIndex = 0;
- }
- }
- }
- render(canvas, x, y) {
- canvas.drawImage(
- this.image,
- this.frameIndex * this.width / this.numberOfFrames,
- 0,
- this.width / this.numberOfFrames,
- this.height,
- x,
- y,
- this.width / this.numberOfFrames,
- this.height
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement