【问题标题】:How to make a sprite move with keyboard in java(libgdx)如何在 java(libgdx) 中使用键盘移动精灵
【发布时间】:2016-01-21 21:35:30
【问题描述】:

我正在为学校动机制作游戏,我已经制作了精灵和背景(平铺地图)我的问题是如何使用键盘使精灵向左、向右、向后和向下移动,请大家帮忙我尽快这是我的代码:

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

}

}

【问题讨论】:

  • 你一定要google这个,这是libGDX中最基本的东西之一,所以网上有很多资源。例如,查看 wiki 上的示例项目:github.com/libgdx/libgdx/wiki/A-simple-game
  • 嗨 :) 我到处搜索,但我不能让我的精灵随键盘移动,我也是编程新手,如果你能帮助我,请帮助:)
  • 在我给你链接的页面上是逐字逐句解释的。

标签: java keyboard libgdx sprite 2d-games


【解决方案1】:

这里我举一个使用键(上、下、左、右)移动精灵的小例子

您应该在 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();
    }
}

【讨论】:

    【解决方案2】:

    要对输入事件做出反应,您需要实现InputProcessor。它有keyDown(按下按键时调用)和keyUp(松开按键时调用)方法。
    这些方法有一个参数keyCode,它定义了按下/释放键的 int 代码。 所以你需要重写这两种方法,并根据你得到的 keyCode,你应该做些什么。 例如,要移动播放器,您可能会保留一个成员speed,您可以根据按下/释放的键设置该成员。在 render 方法中,您需要根据经过的时间 (delta) 和 speed 更新位置。

    要获取输入事件,您需要告诉 libgdx,您的 InputProcessor 应该是活动的。这是通过调用Gdx.input.setInputProcessor(yourInputProcessor) 来完成的。

    另外请务必阅读Libgdx wiki,您的许多问题都会在那里得到解答。

    编辑:
    一些代码:

    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
        }
    }
    

    【讨论】:

    • 非常感谢你 :),你能告诉我添加所有这些东西后我的代码会怎样吗?我是新人 :) 请
    • 我编辑了我的答案。请不要只是复制粘贴代码,而是考虑一下。理解代码很重要,否则你什么也学不会。
    猜你喜欢
    • 2021-03-30
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 2022-05-29
    相关资源
    最近更新 更多