How to make a sprite move with keyboard in java(libgdx) -
i'm making game school motives, have made sprite , background(tiled map) problem how can make sprite move left, right, , down using keyboard, please guys me possible here 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 , map camera.update(); renderer.setview(camera); renderer.render(); //................................................... //tells computer when 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 }
}
here make little example of moving 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 computer when 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(); } }
Comments
Post a Comment