我是新来libgdx,我试图做一个精灵的举动,而相机如下。我可以让精灵完美地移动,直到我将相机连接到它。当我点击时,精灵将移动到它感觉的任何地方(看起来),并且相机将正确地跟随。我尝试了一些不同的东西,但在这一点上它只是猜测和检查。雪碧不能正常移动。 libgdx
public class MyGdxGame implements ApplicationListener {
OrthographicCamera mCamera;
SpriteBatch mBatch;
Texture mTexture, mMap;
Sprite sprite;
float touchX, touchY;
float spriteX, spriteY, speed = 5;
@Override
public void create() {
float CAMERA_WIDTH = 480, CAMERA_HEIGHT = 320;
mBatch = new SpriteBatch();
mTexture = new Texture(Gdx.files.internal("data/logo.png"));
mMap = new Texture(Gdx.files.internal("data/sc_map.png"));
mCamera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
mCamera.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
mBatch.setProjectionMatrix(mCamera.combined);
mCamera.update();
mBatch.begin();
updateInput();
drawD();
mBatch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
public void drawD() {
mCamera.position.set(spriteX, spriteY, 0);
mBatch.draw(mMap, 0, 0);
mBatch.draw(mTexture, spriteX, spriteY);
}
public void updateInput() {
if (Gdx.input.justTouched()) {
touchX = Gdx.input.getX();
touchY = Gdx.input.getY();
}
if (touchX != spriteX) {
if (spriteX < touchX) {
spriteX += speed;
}
if (spriteX > touchX) {
spriteX -= speed;
}
}
if (touchY != spriteY) {
if (spriteY > Gdx.graphics.getHeight() - touchY) {
spriteY -= 10;
}
if (spriteY < Gdx.graphics.getHeight() - touchY) {
spriteY += 10;
}
}
}
}
你能描述更详细的你是什么意思遵循“? – Jyro117 2013-03-18 03:18:02
当用户点击/触摸时,我想获取x和y坐标并让精灵在那里移动。相机应随之移动,因此精灵将始终位于相机的中心。类似于Diablo这样的游戏。 – user2180581 2013-03-18 06:17:42
当我有机会的时候,我会发布一个真实的答案(如果有人不打我的话)。你可以按照自己想要的一种方式在屏幕中心绘制角色,固定相机,然后沿相反方向移动地图/敌人/物体等,而不是移动角色。这给人物移动的幻觉,但保持他们在屏幕的中心。 – Jyro117 2013-03-18 13:03:55