Hi, I’m using ArrayList to store the multiple amounts of images I have. i want to do is if I move left by using arrow keys, the images that pointing towards left move etc. this goes for up, down and right as well.
code inside my player class…
class Gunner{
ArrayList <PImage> images;
int imageCounter = 0;
Gunner(int x, int y)
{
PImage img;
this.x = x;
this.y = y;
//this.speedX = speedX;
//this.speedY = speedY;
images = new ArrayList<PImage>(); //allocates the memory - empty \
for (int count =1; count<=4; count++) //load images into arrayList
{
img = loadImage("soldier" + count +".png");
img.resize(80, 80);
images.add(img);
}
}
void render()
{ //choose next image to display - change every 10th call
int imageNumber = imageCounter/10; //get position of image from arrayList
PImage currentImage = images.get(imageNumber);
imageCounter++;
if (imageCounter==40)
{
imageCounter=0;
}
image(currentImage, this.x, this.y);
}
} //end of class
in main:
PImage background;
int y3=0; //global variable background location
Gunner player; //declare instance of Survivor
void setup()
{
size(600, 400); //size of canvas
background = loadImage("back.jpg");
background.resize(width, height);
player = new Gunner(width/2, height/2); //player starting point, could be chnaged?
} //end of setup
void draw()
{
fill(0);
drawBackground();
drawScorecard();
player.update();
}
void keyPressed() { //possibly could add an pause for my game ?????
if (key == CODED) {
if (keyCode == UP && player.y >= 0 ) //inside top of screen
player.y-=9;
else if (keyCode == DOWN && player.y <= height -90)
player.y+=9;
else if (keyCode == RIGHT && player.x<= width -50)
player.x+=9;
else if (keyCode == LEFT && player.x > 0 +1)
player.x-=9;
}
} //end of keypressed()
void drawBackground() {
image(background, 0, y3); //draw background twice adjacent
image(background, 0, y3-background.height);
} //end of drawBackground
void drawScorecard()
{
fill(255, 255, 0);
textSize(26);
text("Score: " + score, 10, 50);
text(timer.getRemainingTime(), 20, 20); //display seconds remaining top left
}//end of drawScorecard
how would I use ArrayList to determine when my player moves left it will show these images and when my player moves right these images will show??