如何使用libGDX使用键盘键移动精灵? [英] How to move a sprite with the keyboard keys using libGDX?

查看:121
本文介绍了如何使用libGDX使用键盘键移动精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用java和libgdx并拥有此代码,非常简单,它将一个精灵打印到屏幕上。这很有效,我从中学到了很多东西。

i have just started using java and libgdx and have this code, very simply it prints a sprite onto the screen. This works perfectly, and i learnt a lot from it.

package com.MarioGame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.InputProcessor;

public class Game implements ApplicationListener {
private SpriteBatch batch;
private Texture marioTexture;
private Sprite mario;
private int marioX;
private int marioY;

@Override
public void create() {
    batch = new SpriteBatch();
    FileHandle marioFileHandle = Gdx.files.internal("mario.png"); 
    marioTexture = new Texture(marioFileHandle);
    mario = new Sprite(marioTexture, 0, 158, 32, 64);
    marioX = 0;
    marioY = 0;
}

@Override
public void render() {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(mario, marioX, marioY);
    batch.end();
}


@Override
public void resume() {
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void dispose() {
}

}

当用户在键盘上按 D 时,如何修改 marioX 值?

How would I modify the marioX value when a user presses D on their keyboard?

推荐答案

对于您手头的任务,您甚至可能不需要实现InputProcessor。您可以在render()方法中使用Input.isKeyPressed()方法,如下所示。

For your task at hand you might not even need to implement InputProcessor. You can use the Input.isKeyPressed() method in your render() method like this.

float marioSpeed = 10.0f; // 10 pixels per second.
float marioX;
float marioY;

public void render() {
   if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) 
      marioX -= Gdx.graphics.getDeltaTime() * marioSpeed;
   if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) 
      marioX += Gdx.graphics.getDeltaTime() * marioSpeed;
   if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) 
      marioY += Gdx.graphics.getDeltaTime() * marioSpeed;
   if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) 
      marioY -= Gdx.graphics.getDeltaTime() * marioSpeed;

   Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
   batch.begin();
   batch.draw(mario, (int)marioX, (int)marioY);
   batch.end();
}

还要注意我制作马里奥花车的位置坐标并将动作改为基于时间的运动。 marioSpeed是马里奥每秒在任何方向上行进的像素数。 Gdx.graphics.getDeltaTime()返回自上次调用render()以来经过的时间(以秒为单位)。在大多数情况下,实际上不需要转换为int。

Also note that i made the position coordinates of mario floats and changed the movement to time-based movement. marioSpeed is the number of pixels mario should travel in any direction per second. Gdx.graphics.getDeltaTime() returns you the time passed since the last call to render() in seconds. The cast to int is actually not necessary in most situations.

顺便说一句,我们在 http:// www。 badlogicgames.com/forum 你也会问libgdx特定的问题!

Btw, we have forums at http://www.badlogicgames.com/forum where you ask libgdx specific questions as well!

hth,
Mario

hth, Mario

这篇关于如何使用libGDX使用键盘键移动精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆