In my libGDX game I have finally managed to get the sprite to move the way I want him to but for some reason my desired outcome only works the first time I run the code. If I try to run it again, he immediately flies off the screen. If I then comment out one of the brackets, save and then put it back in and run a test, he moves as I want him to (but only the first time). Am I missing something?
EDIT: Ok I changed the code and now I have a new problem, he moves but not smoothly like he does when I move him with the keyboard. now he sort of teleports forwards or backwards about 5pixels every time the screen is clicked (or touched).
Partial Player class:
public class Player extends Entity implements InputProcessor{
OrthographicCamera camera;
Rectangle rectangle;
Vector3 touchPos;
public Player(Vector2 pos, Vector2 direction) {
super(TextureManager.PLAYER, pos, direction);
rectangle = new Rectangle(); //for hitbox
}
@Override
public void update() {
camera = new OrthographicCamera(240, 400);
camera.position.set(240 / 2, 400 / 2, 0);
rectangle.set(pos.x, pos.y + 40, 32, 22); //hitbox
touchPos = new Vector3();
pos.add(direction);{
Gdx.input.setInputProcessor(this);
//the keyboard inputs below work exactly how I want them to
if(Gdx.input.isKeyPressed(Keys.A)) // left
setDirection(-300, 0);
else if(Gdx.input.isKeyPressed(Keys.D)) // right
setDirection(300, 0);
else
setDirection(0, 0);
}
// make sure player stays inside stage
if (pos.x < 0) pos.x = 0;
else if (pos.x > 240 - 32) pos.x = 240 - 32;
}
public void moveLeft() {
setDirection(-300, 0);
}
public void moveRight() {
setDirection(300, 0);
}
public void Still() {
setDirection(0, 0);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
if (screenX < ManicMeltdowns.WIDTH / 2 && screenY > ManicMeltdowns.HEIGHT / 2){
moveLeft();
}
if (screenX > ManicMeltdowns.WIDTH / 2 && screenY > ManicMeltdowns.HEIGHT / 2){
moveRight();
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
if (screenX < ManicMeltdowns.WIDTH / 2 && screenY > ManicMeltdowns.HEIGHT / 2){
Still();
}
if (screenX > ManicMeltdowns.WIDTH / 2 && screenY > ManicMeltdowns.HEIGHT / 2){
Still();
}
return true;
}
Thanks
if (pos.x < 0) pos.x = 0
etc out ofpos.add(direction)
the player immediately flies out of the screen... \$\endgroup\$