Background overwriting character

Hey all, I currently am trying to make a zelda like game. Right now I am working on the character. Currently, I have the background being drawn over the character. Here’s a gif of what I mean: https://gph.is/2oCQdmk

And here’s my code:

PImage characterSprite;
	PImage characterUp, characterDown, characterLeft, characterRight;
	boolean isCharacterUp, isCharacterDown, isCharacterLeft, isCharacterRight;
	String currentSprite;
	int characterX, characterY;
	PImage area1;

	public static void main(String[] args) {
		PApplet.main("zeldaremake.ZeldaRemake");
	}
	
	public void settings() {
		size(768,640);
	}
	
	public void setup() {
		characterSprite = loadImage("character.png");
		characterX = width/2;
		characterY = height/2;
		
		area1 = loadImage("area1.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() {
		image(area1, 0, 0);
		movement();
		
	}
	
	public void movement() {
		if(isCharacterUp || isCharacterDown || isCharacterLeft || isCharacterRight) {
			if(isCharacterUp == true) {
				image(characterSprite.get(0, 0, 32, 32), characterX, characterY);
				characterY--;
			}
			if(isCharacterDown == true) {
				image(characterSprite.get(32, 0, 32, 32), characterX, characterY);
				characterY++;
			}
			if(isCharacterLeft == true) {
				image(characterSprite.get(64, 0, 32, 32), characterX, characterY);
				characterX--;
			}
			if(isCharacterRight == true) {
				image(characterSprite.get(96, 0, 32, 32), characterX, characterY);
				characterX++;
			}
		}
	}

Here are the two images being used:
character

Hi Hedgy,

Did you make sure that you are always drawing a characterSprite?
Maybe some times you never meet any of the conditions? :thinking:

1 Like

You’re right. I just had to add image(characterSprite.get(32, 0, 32, 32), characterX, characterY); in draw() and now its working. Thanks!

2 Likes