精灵无法正确移动. libgdx [英] Sprite not moving properly. libgdx

查看:103
本文介绍了精灵无法正确移动. libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是libgdx的新手,我试图在照相机跟随时让精灵移动.我可以让精灵完美移动,直到将相机连接到它上为止.当我单击时,子画面将移动到任何感觉(看起来),并且照相机将正确跟随.我已经尝试了一些不同的方法,但是在这一点上,它只是猜测和检查.

I'm new to libgdx, I'm trying to make a sprite move while the camera follows. I can make the sprite move perfectly until I attach the camera to it. When I click, the sprite will move wherever it feels like (it seems) and the camera will follow properly. I've tried a few different things but at this point its just guessing and checking.

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

}

}

推荐答案

由于您已经花费了大量的时间并试图使其正常工作,因此,我将为您提供一些帮助,使您更接近所需的内容.查看我所做的更改,下面我将概述为帮助您更好地理解代码所做的工作.

Since you have spent a decent amount of time and are trying to get it working, I will give you a little push forward closer to what you are looking for. Look over the changes I made and below I will outline what I did to help you understand the code better.

  • 正交相机,当您设置相机时,0,0是屏幕的中心.宽度和高度是您在构造函数中指定的值.因此,上边缘将为x,160,下边缘将为x,-160.左边缘为-240,y,右边缘为240,y.
  • drawD()注意,我正在图像的中间绘制精灵,并且它没有移动.相反,我沿相反的方向移动地图(y方向相反).
  • updateInput()注意,我传入了一个delta值(这是帧之间的时间,以秒为单位),这使您可以平滑地移动内容.速度为120,因此可以使角色以120/秒的速度平稳移动.
  • if条件基本上是比较float值的一种简单方法,因此当它足够接近时,它将停止.这样可以防止它过度射击目标位置,然后来回弹跳,因为它可能无法达到准确的值.
  • 我为加载的纹理添加了dispose调用,这些调用很重要,因为您不想填满内存.

我希望这可以帮助您入门并指明正确的方向.在游戏上进行的工作非常耗时,因此请耐心等待,并准备在此过程中学习很多东西!

I hope this helps get you started and pointed in the right direction. Working on games is a lot of work and takes time, so be patient and be ready to learn lots along the way!

根据您的评论,我相信您正在寻找的东西更接近于此:

Based on your comment I believe what you are looking for is something closer to this:

public class MyGdxGame implements ApplicationListener {
    OrthographicCamera mCamera;
    SpriteBatch mBatch;
    Texture mTexture, mMap;
    float touchX, touchY;
    float spriteX, spriteY, speed = 120;

    final float CAMERA_WIDTH = 480, CAMERA_HEIGHT = 320;

    @Override public void create() {
        mCamera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);

        mBatch = new SpriteBatch();
        mTexture = new Texture(Gdx.files.internal("data/logo.png"));
        mMap = new Texture(Gdx.files.internal("data/sc_map.png"));
    }

    @Override public void dispose() {
        mTexture.dispose();
        mMap.dispose();
    }

    @Override public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        updateInput(Gdx.graphics.getDeltaTime());
        mCamera.update();
        mBatch.setProjectionMatrix(mCamera.combined);
        mBatch.begin();
        drawD();
        mBatch.end();
    }

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

    @Override public void pause() {}

    @Override public void resume() {}

    public void drawD() {
        mBatch.draw(mMap, -spriteX - (mMap.getWidth() / 2), spriteY - (mMap.getHeight() / 2));
        mBatch.draw(mTexture, -32, -32, 64, 64);
    }

    public void updateInput(final float delta) {
        if (Gdx.input.justTouched()) {
            touchX = Gdx.input.getX() - (Gdx.graphics.getWidth() / 2);
            touchY = Gdx.input.getY() - (Gdx.graphics.getHeight() / 2);
        }
        final float dv = delta * speed;
        if (Math.abs(touchX - spriteX) > 1) {
            if (spriteX < touchX) {
                spriteX += dv;
            }
            if (spriteX > touchX) {
                spriteX -= dv;
            }
        }
        if (Math.abs(touchY - spriteY) > 1) {
            if (spriteY > touchY) {
                spriteY -= dv;
            }
            if (spriteY < touchY) {
                spriteY += dv;
            }
        }
    }
}

这篇关于精灵无法正确移动. libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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