Hello everybody.
I’m trying to make a simple “game” if it could even be called that, in which i have a character moving in the 4 main directions (up/down/left/right) with the WASD keys, using the following code
void move() {
if (keyPressed) {
if (key=='w')
{
this.y = this.y + this.sy;
}
if (key =='s')
{
this.y = this.y + this.fy;
}
if (key =='a')
{
this.x = this.x + this.sx;
} else if (key=='d')
{
this.x = this.x + this.fx;
}
}
}
I also load the images to an ArrayList with PImage and loop through them to “animate” the character using the following code
ArrayList<PImage> images;
images = new ArrayList<PImage>();
for (int count=1; count<=4; count++)
{
img = loadImage("Wlkawy"+ count + ".png");
images.add(img);
}
void render()
{
int imageNumber = imageCounter/10;
PImage currentImage = images.get(imageNumber);
imageCounter++;
if (imageCounter>=40)
{
imageCounter=0;
}
image(currentImage, this.x, this.y);
}
So i want to change between the 4 different sprite sets: “Wlkawy”, “Wlkfwd”, “Wlklft”, “Wlkrgt”,(there’s 4 images in each sprite set) based on which direction i am moving in.
I have tried to create a new String dir; and change it in the keyPressed if, and setting the img=loadImage to (dir + count + “.png”) but it doesn’t change between the sprites. I have tried setting up 2 booleans walkUp and walkRight, and changing them in the if conition for movement and in a second if condition i would have 4 copies for img=loadImage(“CORRECT NAME” + count + “.png”) with the correct name of the spites, but it doesn’t work. Does anybody have an idea how can i do it without loading any aditional libraries?
Thank you very much for any ideas.