I have a spritesheet for a character and i’d like to be able to split it up in processing. Any way possible?
![]()
Sure. You can use get() with four parameters to grab rectangular regions from it.
PImage sheet;
void setup(){
size(600,400);
sheet = loadImage("sprite_sheet.png");
}
void draw(){
background(0);
image(sheet.get(20, 20, 100, 100), 100, 100);
}
2 Likes
Hi Hedgy,
You can also use the sprites labrary from @quark.
I think you can indicate the number of columns and rows in the constructor and the cuts will be done automatically.
1 Like
This works! Thanks so much.
Heres the code if you feel like reading my sloppy stuff haha:
PImage characterSprite;
PImage characterUp, characterDown, characterLeft, characterRight;
boolean isCharacterUp, isCharacterDown, isCharacterLeft, isCharacterRight;
String currentSprite;
public static void main(String[] args) {
PApplet.main("zeldaremake.ZeldaRemake");
}
public void settings() {
size(768,640);
}
public void setup() {
characterSprite = loadImage("character.png");
}
public void keyPressed() {
switch(key) {
case 'w':
isCharacterUp = true;
break;
case 's':
isCharacterDown = true;
break;
case 'a':
isCharacterLeft = true;
break;
case 'd':
isCharacterRight = true;
break;
}
}
public void keyReleased() {
switch(key) {
case 'w':
isCharacterUp = false;
break;
case 's':
isCharacterDown = false;
break;
case 'a':
isCharacterLeft = false;
break;
case 'd':
isCharacterRight = false;
break;
}
}
public void draw() {
movement();
}
public void movement() {
if(isCharacterUp == true) {
image(characterSprite.get(0, 0, 16, 16), width/2, height/2);
}
if(isCharacterDown == true) {
image(characterSprite.get(16, 0, 16, 16), width/2, height/2);
}
if(isCharacterLeft == true) {
image(characterSprite.get(32, 0, 16, 16), width/2, height/2);
}
if(isCharacterRight == true) {
image(characterSprite.get(48, 0, 16, 16), width/2, height/2);
}
}
}
2 Likes
Thanks so much for sharing your solution, @Hedgy – this is really helpful for people who have similar problems later!
2 Likes