Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to create an entity class for my game but I'm having difficulty with the movement, I can get the sprite moving along the Y-Axis but not along the X-Axis.

Here's my code:-

package Game;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;

public class Entity {

    protected Vector2f pos; // Vector contains a value with components x & y
    protected Rectangle box;
    protected Image sprite;

    public Entity(float x, float y, int width, int height, Image sprite) {
        pos = new Vector2f(x,y);
        box = new Rectangle(x, y, width, height);
        this.sprite = sprite;
    }

    public void update(GameContainer gc, int mapWidth, int mapHeight, int delta) {

    Vector2f trans = new Vector2f(0, 0);

    Input input = gc.getInput();

    if (input.isKeyDown(Input.KEY_W)) // Delta is used to move things on a frame rate independent way
        trans.y = -0.5f * delta;

    if (input.isKeyDown(Input.KEY_S))
        trans.y = 0.5f * delta;

    if (input.isKeyDown(Input.KEY_D))
        trans.x = 0.5f * delta;

    if (input.isKeyDown(Input.KEY_A))
        trans.x = -0.5f * delta;

    if (trans.x != 0 && trans.y != 0) { // If both components aren't null, we reduce them to have constant speed on all directions
        trans.set(trans.x / 1.5f, trans.y / 1.5f);
    }

    if(pos.x+trans.x > 32 && pos.x+trans.x < (mapWidth-64)) // Is the player inside the map? (We add (subtract) because of the stone wall)          pos.x += trans.x;               if(pos.y+trans.y &gt; 32 &amp;&amp; pos.y+trans.y &lt; (mapHeight-64))
        pos.y += trans.y;

}

public void render() {
    sprite.draw(pos.x, pos.y);
}

// Getters and Setters
public Vector2f getPos() {
    return pos;
}

public float getX() {
    return pos.x;
}

public float getY() {
    return pos.y;
}

public void setPos(Vector2f pos) {
    this.pos = pos;
}

public Rectangle getBox() {
    return box;
}

public void setBox(Rectangle box) {
    this.box = box;
}

public Image getSprite() {
    return sprite;
}

public void setSprite(Image sprite) {
    this.sprite = sprite;
}
}

I was just wondering if there's anything glaringly obvious as to why the sprite won't move left and right when the A & D keys are pressed.

Thanks in advance.

share|improve this question

closed as too localized by msell, Byte56, Anko, bummzack, Maik Semder Apr 16 '13 at 6:42

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Too localized in my opinion. "Find my bug" questions are unlikely to help future visitors. See the FAQ about what types of questions to ask here. Good luck with your game. –  Byte56 Apr 15 '13 at 14:18
    
I keep this in mind for future questions. –  GeorgeV Apr 15 '13 at 14:21

1 Answer 1

up vote 3 down vote accepted
if(pos.x+trans.x > 32 && pos.x+trans.x < (mapWidth-64)) // Is the player inside the map? (We add (subtract) because of the stone wall)          pos.x += trans.x;               if(pos.y+trans.y &gt; 32 &amp;&amp; pos.y+trans.y &lt; (mapHeight-64))
        pos.y += trans.y;

That code will move the y component, but you seem to be missing moving x component

you have the correct code commented out right next to it. user error??

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.