gpt4 book ai didi

java - Sprite 无法正常移动。 libgdx

转载 作者:行者123 更新时间:2023-12-02 07:11:22 25 4
gpt4 key购买 nike

我是 libgdx 的新手,我正在尝试在相机跟随时移动 Sprite 。我可以让 Sprite 完美移动,直到我将相机连接到它上面。当我点击时, Sprite 会移动到任何它感觉像(看起来)的地方,并且相机会正确跟随。我尝试了一些不同的方法,但目前只是猜测和检查。

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

}

}

最佳答案

由于您已经花费了相当多的时间并正在努力让它发挥作用,所以我会给您一点插入,让您更接近您想要的东西。查看我所做的更改,下面我将概述我所做的工作以帮助您更好地理解代码。

  • 正交相机,当您设置相机时,0,0 是屏幕的中心。宽度和高度是您在构造函数中指定的。因此,顶部边缘将为 x, 160,底部边缘将为 x, -160。左边缘为 -240, y,右边缘为 240, y。
  • drawD() 请注意,我正在图像中间绘制 Sprite ,但它并未移动。相反,我朝相反方向移动 map (y 反转)。
  • updateInput() 请注意,我传入了一个增量值(这是帧之间的时间(以秒为单位)),这使您可以顺利地移动物体。速度为 120,因此这将以 120/秒的速度平滑地移动你的角色。
  • if 条件基本上是比较浮点值的简单方法,因此当它足够接近时就会停止。这可以防止它过度射击目标位置,然后来回弹跳,因为它可能无法达到准确的值。
  • 我为您加载的纹理添加了 dispose 调用,这些调用很重要,因为您不想填满内存。

我希望这可以帮助您入门并指明正确的方向。制作游戏是一项繁重的工作,需要时间,所以要有耐心,并准备好在这个过程中学习很多东西!

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

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

关于java - Sprite 无法正常移动。 libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com