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
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.
PImage[][] images = new PImage[4][4];//stores all images in an array
int i;
int count;
Player(int x, int y)
{
PImage img;
this.x = x;//store a member called x
this.y = y;
this.Lives = Lives;
for (i=0; x < 4; x++) {
for (count=0; count<4; count++)
{
img = loadImage(dir + count + ".png");
images[i][count] = img;
}
}
}
void render()
{
//int imageNumber = imageCounter/10;
PImage currentImage = images[i][count];
imageCounter++;
if (imageCounter>=40)
{
imageCounter=0;
}
image(currentImage, this.x, this.y);
}
void move() {
if (keyPressed) {
if (key=='w')
{
this.y = this.y + this.sy;
i = 1;
dir = "Wlkawy";
}
if (key =='s')
{
this.y = this.y + this.fy;
i = 2;
dir = "Wlkfwd";
}
if (key =='a')
{
this.x = this.x + this.sx;
i = 3;
dir = "Wlklft";
} else if (key=='d')
{
this.x = this.x + this.fx;
i = 4;
dir = "Wlkrgt";
}
}
}
Is this how it is supposed to work? I get a NullPointerException and i guess that is normal because i have not loaded any images into the 2D array for it to display? How do i get around that, or am i using it correctly at all?
I get a NullPointerExecption at the line image(currentImage, this.x, this.y);