Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. class Sprite {
  2. constructor(options) {
  3. this.image = options.image;
  4. this.frameIndex = 0;
  5. this.tickCount = 0;
  6. this.ticksPerFrame = options.ticksPerFrame || 0;
  7. this.numberOfFrames = options.numberOfFrames || 1;
  8. this.width = options.width;
  9. this.height = options.height;
  10. }
  11.  
  12. update() {
  13. this.tickCount++;
  14. if (this.tickCount > this.ticksPerFrame) {
  15. this.tickCount = 0;
  16. if (this.frameIndex < this.numberOfFrames - 1) {
  17. this.frameIndex++;
  18. } else {
  19. this.frameIndex = 0;
  20. }
  21. }
  22. }
  23.  
  24. render(canvas, x, y) {
  25. canvas.drawImage(
  26. this.image,
  27. this.frameIndex * this.width / this.numberOfFrames,
  28. 0,
  29. this.width / this.numberOfFrames,
  30. this.height,
  31. x,
  32. y,
  33. this.width / this.numberOfFrames,
  34. this.height
  35. );
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement