Android 如何实现libgdx?

Android 如何实现libgdx?,android,touch,libgdx,Android,Touch,Libgdx,我一直坚持使用libgdx实现touchdrable侦听器 这是我的代码,有谁能建议当用户触摸图像而不是移动手指时如何拖动图像 我使用舞台和演员,我想在演员身上捕捉这个事件 谢谢 public void create () { Texture.setEnforcePotImages(false); stage = new Stage(); Gdx.input.setInputProcessor(stage); // create a SpriteBatch wi

我一直坚持使用libgdx实现touchdrable侦听器

这是我的代码,有谁能建议当用户触摸图像而不是移动手指时如何拖动图像

我使用舞台和演员,我想在演员身上捕捉这个事件

谢谢

public void create () {
    Texture.setEnforcePotImages(false);

    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    // create a SpriteBatch with which to render the sprite
    batch = new SpriteBatch();

    // load the sprite's texture. note: usually you have more than
    // one sprite in a texture, see {@see TextureAtlas} and {@see TextureRegion}.
    texture = new Texture(Gdx.files.internal("ball3.png"));
    Skin skin = new Skin();
    skin.add("default", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    skin.add("ball", texture);
    Image sourceImage = new Image(skin, "ball");
    sourceImage.setBounds(50, 125, 100, 100);
    stage.addActor(sourceImage);

    // create an {@link OrthographicCamera} which is used to transform
    // touch coordinates to world coordinates.
    camera = new OrthographicCamera();

    // we want the camera to setup a viewport with pixels as units, with the
    // y-axis pointing upwards. The origin will be in the lower left corner
    // of the screen.
    camera.setToOrtho(false);
}

public void render () {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
    Table.drawDebug(stage);



    // if a finger is down, set the sprite's x/y coordinate.
    if (Gdx.input.isTouched()) {
        // the unproject method takes a Vector3 in window coordinates (origin in
        // upper left corner, y-axis pointing down) and transforms it to world
        // coordinates.
        camera.unproject(spritePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0));
    }
}
以下是步骤:

  • 您需要检查手指是否接触到要移动的对象。以下是获取手指位置的一些有用方法:

    Gdx.input.getX();
    Gdx.input.getY();
    
  • 在触摸时,您需要使用一个变量来跟踪手指是否在移动

  • 如果变量为true,则将对象的位置更改为手指的位置

  • 当手指不再接触屏幕时,禁用变量

  • 以下是步骤:

  • 您需要检查手指是否接触到要移动的对象。以下是获取手指位置的一些有用方法:

    Gdx.input.getX();
    Gdx.input.getY();
    
  • 在触摸时,您需要使用一个变量来跟踪手指是否在移动

  • 如果变量为true,则将对象的位置更改为手指的位置

  • 当手指不再接触屏幕时,禁用变量


  • 我建议您使用InputProcessor:

    public class MyInputProcessor implements InputProcessor{
        @Override
        public boolean keyDown(int keycode){
            return false;
        }
        @Override
        public boolean keyUp(int keycode){
            return false;
        }
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button){
            return false;
        }
        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button){
            return false;
        }
        @Override 
        public boolean keyTyped(char character){
               return false;
        }
        @Override 
        public boolean touchDragged(int screenX, int screenY, int pointer){
            return false;
        }
        @Override 
        public boolean mouseMoved(int screenX, int screenY){
            return false;
        }
        @Override 
                public boolean scrolled(int amount) {
                        return false;
                }   
    }
    
    将其设置为字段:

    MyInputProcessor inputProcessor;
    
    在onCreate()中:


    这样,您就可以在TouchDrawed回调中实现代码。

    我建议您使用InputProcessor:

    public class MyInputProcessor implements InputProcessor{
        @Override
        public boolean keyDown(int keycode){
            return false;
        }
        @Override
        public boolean keyUp(int keycode){
            return false;
        }
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button){
            return false;
        }
        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button){
            return false;
        }
        @Override 
        public boolean keyTyped(char character){
               return false;
        }
        @Override 
        public boolean touchDragged(int screenX, int screenY, int pointer){
            return false;
        }
        @Override 
        public boolean mouseMoved(int screenX, int screenY){
            return false;
        }
        @Override 
                public boolean scrolled(int amount) {
                        return false;
                }   
    }
    
    将其设置为字段:

    MyInputProcessor inputProcessor;
    
    在onCreate()中:

    这样,您就可以在TouchDrawed回调中实现代码