# 2D, Changing sprite set based on direction of movement

**URL:** https://discourse.processing.org/t/2d-changing-sprite-set-based-on-direction-of-movement/25672
**Category:** Coding Questions
**Created:** [November 24, 2020, 7:06pm UTC](https://discourse.processing.org/t/2d-changing-sprite-set-based-on-direction-of-movement/25672 "2020-11-24T19:06:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Myth](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Myth](https://discourse.processing.org/u/Myth)
#### Post date: [November 24, 2020, 7:06pm UTC](https://discourse.processing.org/t/2d-changing-sprite-set-based-on-direction-of-movement/25672/1 "2020-11-24T19:06:46Z")

</div>

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

```auto
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

```auto
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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 24, 2020, 7:19pm UTC](https://discourse.processing.org/t/2d-changing-sprite-set-based-on-direction-of-movement/25672/2 "2020-11-24T19:19:08Z")

</div>

2D array of images

first index = dir, 2nd index = image number this sprite set

`PImage[][] images = new PImage[4][4] ;`

```auto
// load “Wlkawy”
images[0] [0] = loadImage(“Wlkawy1.jpg"); 
images[0] [1] = loadImage(“Wlkawy2.jpg"); 
images[0] [2] = loadImage(“Wlkawy3.jpg"); 
images[0] [3] = loadImage(“Wlkawy4.jpg"); 

// load “Wlkfwd”
images[1] [0] = loadImage(“Wlkfwd1.jpg"); 
images[1] [1] = loadImage(“Wlkfwd2.jpg"); 
images[1] [2] = loadImage(“Wlkfwd3.jpg"); 
images[1] [3] = loadImage(“Wlkfwd4.jpg"); 

// load “Wlklft”
images[2] [0] = loadImage(“Wlklft1.jpg"); 
images[2] [1] = loadImage(“Wlklft2.jpg"); 
images[2] [2] = loadImage(“Wlklft3.jpg"); 
images[2] [3] = loadImage(“Wlklft4.jpg"); 

//Wlkrgt
...
....
...
...

```

---

<div class="post-metadata">

### Author: ![Myth](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Myth](https://discourse.processing.org/u/Myth)
#### Post date: [November 24, 2020, 7:49pm UTC](https://discourse.processing.org/t/2d-changing-sprite-set-based-on-direction-of-movement/25672/3 "2020-11-24T19:49:40Z")

</div>

```auto
  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);`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 24, 2020, 8:28pm UTC](https://discourse.processing.org/t/2d-changing-sprite-set-based-on-direction-of-movement/25672/4 "2020-11-24T20:28:19Z")

</div>

> [@Myth](#):
>
> i guess that is normal because i have not loaded any images into the 2D array for it to display?

YES.

Just load them as I have shown above.

> [@Chrisir](#):
>
> ```auto
> // load “Wlkawy”
> images[0] [0] = loadImage(“Wlkawy1.jpg"); 
> images[0] [1] = loadImage(“Wlkawy2.jpg"); 
> images[0] [2] = loadImage(“Wlkawy3.jpg"); 
> images[0] [3] = loadImage(“Wlkawy4.jpg"); 
> 
> // load “Wlkfwd”
> images[1] [0] = loadImage(“Wlkfwd1.jpg"); 
> images[1] [1] = loadImage(“Wlkfwd2.jpg"); 
> images[1] [2] = loadImage(“Wlkfwd3.jpg"); 
> images[1] [3] = loadImage(“Wlkfwd4.jpg"); 
> 
> // load “Wlklft”
> images[2] [0] = loadImage(“Wlklft1.jpg"); 
> images[2] [1] = loadImage(“Wlklft2.jpg"); 
> images[2] [2] = loadImage(“Wlklft3.jpg"); 
> images[2] [3] = loadImage(“Wlklft4.jpg"); 
> 
> //Wlkrgt
> ...
> ....
> ...
> ...
> 
> ```
