xxxxxxxxxx
77
let spritesheet;
let sprites = [];
let direction = 1; // 0 up
let step = 0;
let x;
let y;
let speed = 5;
function preload() {
spritesheet = loadImage("walking.png");
}
function setup() {
// fullscreen(true);
createCanvas(500, 450);
// 12 images across, 4 down, in the spritesheet
let w = int(spritesheet.width / 12);
let h = int(spritesheet.height / 4);
for (let y = 0; y < 4; y++) {
sprites[y] = [];
for (let x = 0; x < 12; x++) {
sprites[y][x] =
spritesheet.get(x * w, y * h, w, h);
} // iterate over rows
} // iterate over columns
x = width / 2;
y = height / 2;
imageMode(CENTER);
// Display first sprite
image(sprites[direction][step], x, y);
}
// nothing to do here because all the action
// happens in the keyPressed() callback
function draw() {}
function keyPressed() {
// look at sprite sheet to determine
// which direction is which row
if (key === 's') {
direction = 0;
y += speed;
}
if (key === 'a') {
direction = 1;
x -= speed;
}
if (key === 'd') {
direction = 2;
x += speed;
}
if (key === 'w') {
direction = 3;
y -= speed;
}
// Every so often
// advance to the next sprite
if (frameCount % 1 == 0) {
step = (step + 1) % 12;
}
// Finally draw paint the sprite
background(255);
image(sprites[direction][step], x, y);
}