# How do I load specific parts of an image, like if I was using a sprite sheet? I was told that I could use 'get();' in order to grab a part of the image, but I don't know how to do that. Where do I include the code for this? (Sprite she

**URL:** https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714
**Category:** Coding Questions
**Created:** [January 26, 2023, 3:18am UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714 "2023-01-26T03:18:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![FelixBrooks](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@FelixBrooks](https://discourse.processing.org/u/FelixBrooks)
#### Post date: [January 26, 2023, 3:18am UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/1 "2023-01-26T03:18:21Z")

</div>

\<PImage Walking;  
color c - color(0);  
void setup();  
background(c);  
noStroke();  
fill(c);  
Walking = loadImage('Walking-Animation.png");

void draw();  
display();  
image(Walking, pos.x, pos.y);  
// This is where I’m stuck. I thought I could get 1/6th of the image’s width (because there’s 6 images on the sheet), but it does nothing.  
Walking.get(0, 0, Walking.width/6, Walking.height);  
void display ();  
background(c);  
move();

>

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [January 26, 2023, 5:31am UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/2 "2023-01-26T05:31:09Z")

</div>

The `get()` function is returning a PImage. You should assign that image to a PImage variable.

You should also do this in `setup()`, because you only want to “cut” the image once, since that image does not change.

Your code also has… other problems. If you are trying to use setup() and draw() functions, you need curley braces around the instructions in their bodies:

```auto
PImage Walking;
PImage cut;
color c = color(0);

void setup(){
  background(c);
  noStroke();
  fill(c);
  Walking = loadImage("Walking-Animation.png");
  cut = Walking.get(0, 0, Walking.width/6, Walking.height);
}

void draw(){
  display();
  image(cut, pos.x, pos.y);
}

void display (){
  background(c);
  move();
}

```

---

<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: [January 26, 2023, 5:48am UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/3 "2023-01-26T05:48:43Z")

</div>

When your walking animation has 6 images (frames) on the sheet, you need an array to store them (the walk animation).

**How to do it**

So this line

> [@TfGuy44](#):
>
> ` cut = Walking.get(0, 0, Walking.width/6, Walking.height);`

must be placed in a for-loop (example for when all 6 images are _next to each other_ in the initial image) :

```auto
for (int i=0; i<6; i++) {
    cut[i] = Walking.get( i * (Walking.width/6.0), 0, Walking.width/6, Walking.height); 
}

```

`cut` must be an array

```auto
    PImage[] cut = new PImage[6];

```

**Animation**

When we animate, you would loop `cut[]`:

```auto
image(cut[frame], playerX, playerY);
frame++; // or use a timer with millis() to slow it down
if (frame>=6) 
    frame = 0;

```

**Different kind of animations**

Sometimes, there are even different animations, like walk left/right, stand jump land, fire…

if this is so, you need

- a 2D array `cut[][]` and
- use state (like walk left/right, stand jump land, fire…) and frame variable: `cut[state][frame]`
- you set `state` when he makes the movement like walk left, walk right, jump…
- you set `frame` like above. Presumably you reset frame to 0 every time you set state.

Chrisir

---

<div class="post-metadata">

### Author: ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)
#### Post date: [January 26, 2023, 2:49pm UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/4 "2023-01-26T14:49:42Z")

</div>

While I havent personally played around with the idea, I’ve seen the copy function being used for this aswell

---

<div class="post-metadata">

### Author: ![FelixBrooks](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@FelixBrooks](https://discourse.processing.org/u/FelixBrooks)
#### Post date: [January 26, 2023, 5:49pm UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/5 "2023-01-26T17:49:22Z")

</div>

I do, I just forgot to add them in the question lmao.

---

<div class="post-metadata">

### Author: ![FelixBrooks](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@FelixBrooks](https://discourse.processing.org/u/FelixBrooks)
#### Post date: [January 26, 2023, 6:45pm UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/6 "2023-01-26T18:45:20Z")

</div>

Chrisir, it said that get expects parameters like ‘get(int, int, int, int);’  
Rather than ‘get(float, int, int, int, int);’

---

<div class="post-metadata">

### Author: ![FelixBrooks](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@FelixBrooks](https://discourse.processing.org/u/FelixBrooks)
#### Post date: [January 26, 2023, 6:49pm UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/7 "2023-01-26T18:49:19Z")

</div>

It also said that the variable ‘frame’ does not exist.

---

<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: [January 26, 2023, 8:05pm UTC](https://discourse.processing.org/t/how-do-i-load-specific-parts-of-an-image-like-if-i-was-using-a-sprite-sheet-i-was-told-that-i-could-use-get-in-order-to-grab-a-part-of-the-image-but-i-dont-know-how-to-do-that-where-do-i-include-the-code-for-this-sprite-she/40714/8 "2023-01-26T20:05:41Z")

</div>

You have to debug your code. Proof reading it etc.

**First**

> [@FelixBrooks](#):
>
> it said that get expects parameters like ‘get(int, int, int, int);’  
> Rather than ‘get(float, int, int, int, int);’

> [@Chrisir](#):
>
> ` cut[i] = Walking.get( i * (Walking.width/6.0), 0, Walking.width/6, Walking.height);`

these are 4 parameters (why did he mention Rather than “get(float, int, int, int, int)” ? )

Try using int() command on the first parameter.

```auto
     cut[i] = Walking.get( int ( i * (Walking.width/6.0) ) , // use int () here maybe; use * sign here 
                           0, 
                           Walking.width/6, 
                           Walking.height);

```

* * *

**Second**

> [@FelixBrooks](#):
>
> It also said that the variable ‘frame’ does not exist.

That’s right, you need to declare new variables.

Please post your entire code to make progress.

Chrisir
