Codersec
Codersec

Reputation: 31

How to make a sprite move with keyboard in java(libgdx)

i'm making a game for school motives, i have already made the sprite and the background(tiled map) my problem is how i can make the sprite move left, right, back and down using the keyboard, please guys help me as soon as possible here is my code:

public class LEVEL1 implements ApplicationListener, Screen {
private Music music;
private SpriteBatch batch;
private Texture Sprite;
private Vector2 position;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;

@Override
public void render(float delta) {       
    Gdx.gl.glClearColor(1, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    position.y = position.y - 5;

    // player Controls
    if(position.y < 0){
        position.y = 0;
    }

    //..................................................

    // renderer camera and map
    camera.update();
    renderer.setView(camera);
    renderer.render();
    //...................................................

//tells the computer when to start drawing textures
    batch.begin();
    batch.draw(Sprite, position.x, position.y, 50, 50);
    batch.end();
    //...................................................

    camera = new OrthographicCamera();
    camera.setToOrtho(true, 2920,950);

}

@Override
public void show() {    
    Sprite = new Texture("Sprite.png");
    batch = new SpriteBatch();
    position = new Vector2(650, Gdx.graphics.getHeight());

    map = new TmxMapLoader().load("map1.tmx");

    renderer = new OrthogonalTiledMapRenderer(map);

    camera = new OrthographicCamera();

      music = Gdx.audio.newMusic((Gdx.files.internal("GameSound.mp3")));

        music.setLooping(false);
        music.setVolume(0.5f);
        music.play();
}

@Override
public void create() {

}

@Override
public void resize(int width, int height) {
    camera.viewportWidth = width;
    camera.viewportHeight = height;
    camera.position.set(width/2f, height/3f, 0); //by default camera position on (0,0,0)
    camera.update();


}

@Override
public void render() {
    if(Gdx.input.justTouched())
        music.play();


}

@Override
public void dispose() {
    map.dispose();
    renderer.dispose();
    music.dispose();        
}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

}

Upvotes: 1

Views: 1930

Answers (2)

Robert P
Robert P

Reputation: 9783

To react on input events you need to implement InputProcessor. It has the methods keyDown (called when a key is pressed) and keyUp (called when a key is released).
Those methods have an argument keyCode, which defines the int code of the pressed/released key. So you need to override this 2 methods and depending on the keyCode you get, you should do something or not. To move the player for example, you might keep a member speed, which you set, depending on the pressed/released key. In the render method you then need to update the position depending on the elapsed time (delta) and the speed.

To get the input event, you need to tell libgdx, that your InputProcessor should be the active one. This is done by calling Gdx.input.setInputProcessor(yourInputProcessor).

Also make sure to read the Libgdx wiki, many of your questions will be answered there.

EDIT:
Some code:

public class Level implements ApplicationListener, InputProcessor {

    private int speedX;  // Speed in x direction
    private Vector2 position;  // Position

    private boolean keyDown(int keyCode) {
        boolean result = false;
        if (keyCode == Keys.D) {
           result = true;
           speed += 5;
        }
        else if (keyCode == Keys.A) {
            result = true;
            speed -= 5;
        }
        return result;
    }

    private boolean keyUp(int keyCode) {
        boolean result = false;
        if (keyCode == Keys.D) {
           result = true;
           speed -= 5;
        }
        else if (keyCode == Keys.A) {
            result = true;
            speed += 5;
        }
        return result;
    }

    public void render(float delta) {
         position.x += speed*delta;

         // Render here
    }
}

Upvotes: 1

Netero
Netero

Reputation: 3819

Here i make a little example of moving a sprite using keys (up,down, left,right)

you should find more details in libgdx wiki

public class Level1 implements ApplicationListener {


    Sprite sprite;
    SpriteBatch batch;

    float spriteXposition;
    float spriteYposition;

    @Override
    public void render() { 

        Gdx.gl.glClearColor(1, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //tells the computer when to start drawing textures
        batch.begin();
        sprite.setPosition(spriteXposition, spriteYposition);
        sprite.draw(batch);
        batch.end();

        spriteControl();

    }

    public void spriteControl() {

        if(Gdx.input.isKeyPressed(Keys.UP)) {
            spriteYposition++;
        }
        if(Gdx.input.isKeyPressed(Keys.DOWN)) {
            spriteYposition--;
        }

        if(Gdx.input.isKeyPressed(Keys.LEFT)) {
            spriteXposition--;
        }

        if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
            spriteXposition++;
        }
    }


    @Override
    public void create() {

        sprite = new Sprite(new Texture(Gdx.files.internal("sprite.png")));
        batch = new SpriteBatch();
    }
}

Upvotes: 1

Related Questions