【问题标题】:libgdx drag and droplibgdx 拖放
【发布时间】:2014-01-29 09:25:15
【问题描述】:

我正在尝试向 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);
}

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    我在我的一个项目中实现了您的代码。

    我删除了您的渲染并使用了下面的渲染。此外,您的图像导入不需要 assets/ 前缀。

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

    【讨论】:

    • IMO 在调用Stage#draw() 之前调用Stage#act() 更符合逻辑。您更新视图的状态,然后绘制它们,而不是相反。
    【解决方案2】:

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

    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() {
        }       
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多