ArrayList - having different images when moving using arrow keys

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??

Summary,

if i move left, show images that are moving left
if i move right, show images that are moving right
etc…

you need to store for each Image individually where it is pointing.

So make a new class PointingImage:

PImage image;
int pointingDirection  = 0; // 0 is up, 1, east, 2 south, 3 west 

Replace

ArrayList <PImage> images;

with

when a key is pressed loop over the images and display only where the key matches the pointingDirection

1 Like

how would i call that in keypressed?

would it be player.PointingImage

make a global var pointingDirectionFromKey

if(key==UP)
pointingDirectionFromKey=0; // North

etc.

and then when display the images check if(image.pointingDirection == pointingDirectionFromKey)

1 Like

if i wanted to do it for a class which doesnt use keypressed()
how would I do that?

eg have an object that moves on its own.
would that be inside the object’s class in void move()

class Gunner{
      ArrayList <PointingImage> images; // !!!!!!!!!!!!!!
1 Like

im sorry im confused, i still dont understand lot of arraylist and how it works. the fact that its in my class means what exactly to me??

make a new class PointingImage as I described

class Gunner{
      ArrayList <PointingImage> images; // !!!!!!!!!!!!!!

and then player.images.get(0).image;

Chrisir

1 Like

thats only getting 1 image,

could i do:
player.images.get(1-4).image;

Yes, in a for loop

see reference ArrayList

1 Like

ahhh, i get it. thank you so much. love you(no homo) <3

1 Like

Show your entire code and your current attempt to get real help.

YOU need to write the code and to test it.

1 Like

ill write my code and if i get stuck, ill reply back on here.

1 Like

so when you load your images - presumably in the constructor of Gunner class

you say something like add the new image name “xy” to the ArrayList and it’s facing left.

You need to do this “manually”, so write a new line for each image because you have to decide for each image if its pointingDirection is 0,1,2 or 3

1 Like