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

<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();

1 Like

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:

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();
}
4 Likes

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

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

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

    PImage[] cut = new PImage[6];

Animation

When we animate, you would loop cut[]:

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

1 Like

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

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

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

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

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

First

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

Try using int() command on the first parameter.

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

Second

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

Please post your entire code to make progress.

Chrisir

1 Like