libgdx拖放

时间:2014-01-29 09:25:15

标签: java libgdx

我试图在Libgdx中为多个图像添加拖放功能。我看过这个例子:https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/DragAndDropTest.java但它还没有用。图像不会拖放。有人能够给我一些指示,说明为什么它不起作用? 感谢

private void createButton() {
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    skin = new Skin();
    skin.add("up", new Texture(Gdx.files.internal("assets/data/up.png")));
    skin.add("def", new Texture(Gdx.files.internal("assets/data/Goal.png")));

    final Image up = new Image(skin, "up");
    up.setBounds(1090, 630, 40, 40);
    stage.addActor(up);
    Image def = new Image(skin, "def");
    def.setBounds(1090, 585, 40, 40);
    stage.addActor(def);
    DragAndDrop dragAndDrop = new DragAndDrop();
    dragAndDrop.addSource(new Source(up) {
            public Payload dragStart (InputEvent event, float x, float y, int pointer) {
                    Payload payload = new Payload();
                    payload.setObject(payload);
                    payload.setDragActor(up);
                   payload.setDragActor(new Label("up", skin));
                    Label validLabel = new Label("up", skin);
                    validLabel.setColor(0, 1, 0, 1);
                    payload.setValidDragActor(validLabel);

                    return payload;
            }
    });

    dragAndDrop.addTarget(new Target(def) {
        public boolean drag (Source source, Payload payload, float x, float y, int pointer) {
                getActor().setColor(Color.GREEN);
                return true;
        }

        public void reset (Source source, Payload payload) {
                getActor().setColor(Color.WHITE);
        }

        public void drop (Source source, Payload payload, float x, float y, int pointer) {
                System.out.println("Accepted: " + payload.getObject() + " " + x + ", " + y);
        }
});
    render();
}

  public void render () {
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
        Table.drawDebug(stage);
}

2 个答案:

答案 0 :(得分:3)

我在我的项目上实现了你的代码。

我删除了你的渲染并使用了下面的渲染。此外,您不应该需要资源/前缀来导入图像。

skin.add("up", new Texture(Gdx.files.internal("images/coin.png")));
skin.add("def", new Texture(Gdx.files.internal("images/coin.png")));

 @Override
public void render(float delta) {
    super.render(delta);
    stage.draw();
    stage.act(Gdx.graphics.getDeltaTime());

}

答案 1 :(得分:-1)

另一种以更好的方式拖动的方法......

public class CaveInterection implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture bgTexture;
    private Sprite sprite;
    private Stage stage;
    private Texture mirrTexture;
    private MyActor mirrorActor;
    Sprite img1,img2;
    @Override
    public void create() {      
        camera = new OrthographicCamera(1024, 550);
        camera.position.set(1024 / 2, 550 / 2, 0);
        batch = new SpriteBatch();
        stage = new Stage(1024, 550, false);
        //bgTexture = new Texture(Gdx.files.internal("data/cave.jpg"));
        //bgTexture = new Texture(Gdx.files.internal("data/bg.jpg"));

        mirrTexture = new Texture(Gdx.files.internal("data/mirror.png"));
        mirrTexture
        .setFilter(TextureFilter.Linear, TextureFilter.Linear);
        mirrorActor = new MyActor(new TextureRegion(mirrTexture));
        mirrorActor.setPosition(700, 400);
        mirrorActor.setOrigin(mirrorActor.getWidth()/2, mirrorActor.getHeight()/2);
        stage.addActor(mirrorActor);


        // finally stage as the input process 
        Gdx.input.setInputProcessor(stage);

    }

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

    @Override
    public void render() {      
        // clear the screen, update the camera and make the sprite batch
        // use its matrices.
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        //batch.draw(bgTexture, 0,0);
    //  Gdx.app.log("arvind","X :"+mirrorActor.getX()+ " Y :"+mirrorActor.getY());
        //batch.draw(bgTexture, 0, 0);
        batch.end();
        // tell the stage to act and draw itself
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    public class MyActor extends Actor {
        TextureRegion region;
        float lastX;
        float lastY;

        public MyActor (TextureRegion region) {
            this.region = region;
            setWidth(region.getRegionWidth());
            setHeight(region.getRegionHeight());

            addListener(new InputListener() {
                public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                    Gdx.app.log("arv", "pointer1+"+pointer);
                    // we only care for the first finger to make things easier
                    if (pointer != 0) return false;

                    // record the coordinates the finger went down on. they
                    // are given relative to the actor's   corner (0, 0)
                    Gdx.app.log("arvind", "touchDown");
                    Gdx.app.log("arv", "pointer2+"+pointer+""+x+"::::"+y);
                    lastX = x;
                    lastY = y;
                    return true;
                }

                public void touchDragged (InputEvent event, float x, float y, int pointer) {
                    // we only care for the first finger to make things easier
                    if (pointer != 0) return;
                    Gdx.app.log("arv", "touchDragged");
                    // adjust the actor's position by (current mouse position - last mouse position)
                    // in the actor's coordinate system.
                translate(x - lastX, y - lastY);
                //   rotate(2);
                    // save the current mouse position as the basis for the next drag event.
                    // we adjust by the same delta so next time drag is called, lastX/lastY
                    // are in the actor's local coordinate system automatically.
                    lastX = x - (x - lastX);
                    lastY = y - (y - lastY);
                }
            });
        }

        @Override
        public void draw (SpriteBatch batch, float parentAlpha) {
            //batch.draw(region, getX(), getY());
            batch.draw(region, getX(), getY(), mirrorActor.getOriginX(), mirrorActor.getOriginY(), mirrorActor.getWidth(), mirrorActor.getHeight(), 1, 1,getRotation(), true);
        }
    }

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

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }       
}