(Marte Engine, Slick2d) Basic Player Movement Problem

I have decided to use the MarteEngine to make my game and things are going well but in the player class for some reason my player “Entity” wont move the correct sprite image for the direction key pressed works. so the player will rotate but not move from the location. please any help with this will would be appreciated ive searched google and forums and nothing so far.

Player Class:

import it.randomtower.engine.ResourceManager;
import it.randomtower.engine.entity.Entity;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;


public class Player extends Entity {
	
	public static String name = "player";
	public final static String UP = "up";
	public final static String DOWN = "down";
	public final static String LEFT = "left";
	public final static String RIGHT = "right";
	public final static int DIRECTION_UP=0;
	public final static int DIRECTION_DOWN=1;
	public final static int DIRECTION_LEFT=2;
	public final static int DIRECTION_RIGHT=3;
	public float x;
	public float y;
 	public  float speed = 1f;
	private final int tileSize = Attributes.TILE_SIZE;
	public Image currentImg;
	public Image up;
	public Image down;
	public Image left;
	public Image right;

	public Player(float x, float y) {
		super(x, y);
		this.down = (ResourceManager.getSpriteSheet("player").getSubImage(0, 0 * tileSize, tileSize, tileSize));
		this.up = (ResourceManager.getSpriteSheet("player").getSubImage(0, 3 * tileSize, tileSize, tileSize));
		this.left = (ResourceManager.getSpriteSheet("player").getSubImage(0, 1 * tileSize, tileSize, tileSize));
		this.right = (ResourceManager.getSpriteSheet("player").getSubImage(0, 2 * tileSize, tileSize, tileSize));
		this.setGraphic(down);
		this.setHitBox(0, 0, tileSize , tileSize );
		this.addType(name);
		defineControls();
		
	}
	
	private void defineControls() {
		define(UP, Input.KEY_W, Input.KEY_UP, Input.KEY_W);
		define(DOWN, Input.KEY_S, Input.KEY_DOWN, Input.KEY_S);
		define(LEFT, Input.KEY_A, Input.KEY_LEFT, Input.KEY_A);
		define(RIGHT, Input.KEY_D, Input.KEY_RIGHT, Input.KEY_D);
	}
	

	@Override
	public void update(GameContainer container, int delta)
			throws SlickException {
		super.update(container, delta);
		Input input = container.getInput();
		if (input.isKeyPressed(Input.KEY_UP)||pressed(UP)||input.isKeyPressed(Input.KEY_W)) {
			y -= 1;
			setGraphic(up);
			this.setPosition(x,y);
		} else if (input.isKeyPressed(Input.KEY_DOWN)||pressed(DOWN)||input.isKeyPressed(Input.KEY_S)) {
			y += 1;
			setGraphic(down);
			this.setPosition(x,y);
		} else if (input.isKeyPressed(Input.KEY_LEFT)||pressed(LEFT)||input.isKeyPressed(Input.KEY_A)) {
			x += 1;
			setGraphic(left);
			this.setPosition(x,y);
		} else if (input.isKeyPressed(Input.KEY_RIGHT)||pressed(RIGHT)||input.isKeyPressed(Input.KEY_D)) {
			x -= 1;
			setGraphic(right);
			this.setPosition(x,y);
		}
		
		
	}

	private void setPosition(float x2, float y2) {
		this.x = x2;
		this.y = y2;
	}
}

bump

How is the sprite drawn? If I were to take a guess, judging by the code you posted, it’s probably because the position should be handled in the superclass, however you’re handling it in the Player class.


private void setPosition(float x2, float y2) {
      this.x = x2;
      this.y = y2;
}

Shouldn’t you be calling super.x?
You’re passing the position to the superclass constructor upon creation of the object, so why are you handling the position in the sub-class?

Thanks!!!

I really appreciate it i have been stuck for a long time and i new it was something very simple like that. do you think i should change those to super.x, super.y or make a constructor that doesn’t call the super. do you think that will cause problems later for me if i make classes that extend “Player” and try and assign values to that object that arent inherited by the super class?

If every entity holds a position (which I suspect is the case), then handle the position within the Entity class.

You should read up on OOP designs, and think logically about what an object should have. An Entity, for example, is an object that exists with a position, but does not need to rendered. So what should Entity handle? Logically:

-Position
-Updating position

In it’s most basic form. Now you can create a separate class that extends Entity. What would this class be? The “Mob” class. A “Mob” can be rendered, can have a position, can move etc… So it would be logical to assume that a “Mob” is also an Entity. But the Mob can also be rendered, so you will now have to have a way to access the graphics object to render the player. But Mob shouldn’t have to touch the position in Entity (at least a basic Mob class implementation shouldn’t), so you should keep the position in the Entity class, where Mob can call upon it.