Hello Arduboy Community!
I was following the tutorials on this website and tried to make my own game using the skills found there. I was trying to make my sprite go through 3 different images: a standing image, a running image, and another running image. The code starts with the standing image. When the Right button is pressed, I want it to change to the first running image. It is supposed to file through the first and second running images, and go and stay at the standing image when the right button is not pressed anymore. When I run it, however, it only stays on the standing image.
Am I not going through the sprites correctly? Or did I misunderstand the tutorials and should read through them again?
Here is the code:
#include "images.h"
#define PLAYER_WIDTH 10
#define PLAYER_HEIGHT 16
#define PLAYER_X_OFFSET WIDTH / 2 - PLAYER_WIDTH / 2
#define PLAYER_Y_OFFSET HEIGHT / 2 - PLAYER_HEIGHT / 2
enum Stance {
Standing,
Running1,
Running2
};
const byte *player_images[] { player_standing, player_running1, player_running2 };
struct Player {
int x;
int y;
Stance stance;
bool sliding;
const byte *image;
};
Player player = {PLAYER_X_OFFSET, PLAYER_Y_OFFSET, 0, false, player_standing};
void drawplayer() {
player.image = player_images[player.stance];
arduboy.fillRect(PLAYER_X_OFFSET, PLAYER_Y_OFFSET, PLAYER_WIDTH, PLAYER_HEIGHT, BLACK);
Sprites::drawExternalMask(player.x, player.y, player.image, 0);
if (arduboy.pressed(RIGHT_BUTTON)) {
switch(player.stance) {
case Stance::Standing:
player.stance = Stance::Running1;
break;
case Stance::Running1:
player.stance = Stance::Running2;
break;
case Stance::Running2:
player.stance = Stance::Running1;
}
if (arduboy.notPressed(RIGHT_BUTTON) && player.stance == Stance::Running1 || player.stance == Stance::Running2) {
player.stance = Stance::Standing;
}
}
}
}
}
And here is the image data for the sprites:
const byte PROGMEM player_standing[] = {
//Standing
10, 16,
0x78, 0x84, 0x02, 0x29, 0x41, 0x41, 0x29, 0x02, 0x84, 0x78, 0x00, 0x3c, 0x85, 0xfe, 0x1e, 0x1e,
0xfe, 0x85, 0x3c, 0x00
};
const byte PROGMEM player_running1[] = {
////Running1
10, 16,
0x78, 0x84, 0x02, 0x01, 0x01, 0x21, 0x49, 0x42, 0x84, 0x78, 0x00, 0x18, 0x85, 0x7e, 0x1e, 0x1e,
0xfe, 0x85, 0x08, 0x04,
};
const byte PROGMEM player_running2[] = {
//Running2
10, 16,
0x78, 0x84, 0x02, 0x01, 0x01, 0x21, 0x49, 0x42, 0x84, 0x78, 0x00, 0x18, 0x85, 0x7e, 0xfe, 0xfe,
0x9e, 0x05, 0x08, 0x04,
};
Note: This code is part of a bigger game so let me know if you want to see more code.
Thanks a lot!