-2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – House
    Apr 15, 2013 at 14:18
  • \$\begingroup\$ I keep this in mind for future questions. \$\endgroup\$
    – GeorgeV
    Apr 15, 2013 at 14:21

1 Answer 1

3
\$\begingroup\$
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??

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .