Skip to content

Instantly share code, notes, and snippets.

@raduGaspar
Last active November 12, 2016 13:07
Show Gist options
  • Save raduGaspar/1d1e8e2b5d0ce8607a3901d0fe0b4662 to your computer and use it in GitHub Desktop.
Save raduGaspar/1d1e8e2b5d0ce8607a3901d0fe0b4662 to your computer and use it in GitHub Desktop.
export default class SpriteSheet {
constructor(img, spriteWidth=16, spriteHeight=spriteWidth) {
let i;
let j;
let blocksW = img.width / spriteWidth;
let blocksH = img.height / spriteHeight;
let sprites = [];
this.img = img;
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
this.sprites = sprites;
for(i=0; i<blocksH; i++) {
for(j=0; j<blocksW; j++) {
sprites.push([
j * spriteWidth,
i * spriteHeight,
spriteWidth,
spriteHeight
]);
}
}
}
getTile(row, col) {
return [
row * this.spriteWidth,
col * this.spriteHeight,
this.spriteWidth,
this.spriteHeight
];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment