Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用鼠标移动图片时会偏离中心_Android_Eclipse_Libgdx - Fatal编程技术网

Android 用鼠标移动图片时会偏离中心

Android 用鼠标移动图片时会偏离中心,android,eclipse,libgdx,Android,Eclipse,Libgdx,我显示了一张图片,但当我试图用鼠标移动它时,我的鼠标偏离了图片的中心。基本上,当我的鼠标位于应用程序屏幕的中心时,图像位于屏幕的右上角。另一种解释是,当我移动鼠标时,鼠标不是直接在图像上方,而是移到一边 下面是它的主要代码 public class SlingshotSteve implements ApplicationListener{ private OrthographicCamera camera; private SpriteBatch batch; private Texture

我显示了一张图片,但当我试图用鼠标移动它时,我的鼠标偏离了图片的中心。基本上,当我的鼠标位于应用程序屏幕的中心时,图像位于屏幕的右上角。另一种解释是,当我移动鼠标时,鼠标不是直接在图像上方,而是移到一边

下面是它的主要代码

public class SlingshotSteve implements ApplicationListener{

private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;


@Override
public void create() { 
camera = new OrthographicCamera(1280, 720);     

batch = new SpriteBatch();


texture = new Texture(Gdx.files.internal("image.png"));
sprite = new Sprite(texture);

sprite = new Sprite(texture);
sprite.setOrigin(0,0);
sprite.setPosition(-sprite.getWidth()/2,-sprite.getHeight()/2);


}

@Override
public void dispose() {
batch.dispose();
texture.dispose();
}

@Override
public void render() {  
// Make the background color Black
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

// Mouse imput        
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
    sprite.setPosition(Gdx.input.getX() - sprite.getWidth()/2,
            Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getHeight()/2);
}
if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
    sprite.setPosition(Gdx.graphics.getWidth()/2 -sprite.getWidth()/2, 
            Gdx.graphics.getHeight()/2 - sprite.getHeight()/2);
}

batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}

我认为这是正交相机的问题,但我不确定,即使我知道,我也不知道如何修复它。

因为你可能忘了打开坐标

Vector3 tmp=new Vector3(Gdx.input.getX(),Gdx.input.getY(),0);
camera.unproject(tmp);
float x=tmp.x,y=tmp.y;
现在你可以任意使用x和y

if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
sprite.setPosition(x - sprite.getWidth()/2,
       y - sprite.getHeight()/2);
}


并移除雪碧。setOrigin(0,0);你不需要它。

我如何使用X和Y?我应该放什么?当你说你想用x和y的时候,我该怎么用呢?对不起,我是新手。X和y现在是你的精灵的世界位置。您已将屏幕坐标转换为世界坐标。X和Y是鼠标在屏幕上的位置