当drop drop -libgdx时,Actor会丢失

时间:2014-02-03 09:35:11

标签: java drag-and-drop libgdx

我正在进行一场朗姆酒游戏,我有一块有瓷砖的棋盘,船上的所有位置都被定义为TileActor。一些瓷砖演员有实际的瓷砖,有些是空的。问题是:当我拖动一个瓷砖并将其放在一个空的TileActor上时,它工作正常但是当我把它放在空的空间时,演员就迷路了。如何防止演员迷路呢?

这是我的拖放代码:

dragAndDrop.addSource(new Source(actor) {   
        @Override
        public Payload dragStart(InputEvent event, float x, float y, int pointer) {
            Payload payload = new Payload();
            payload.setObject("Some payload!");
            payload.setDragActor(getActor());

            Label validLabel = new Label("Valid move!", skin);
            validLabel.setColor(0, 1, 0, 1);
            payload.setValidDragActor(validLabel);

            Label invalidLabel = new Label("Invalid!", skin);
            invalidLabel.setColor(1, 0, 0, 1);
            payload.setInvalidDragActor(invalidLabel);


            return payload;
        }

    dragAndDrop.addTarget(new Target(actor) {
                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) {

                    int movingTile =((TileActor)source.getActor()).getPosition();
                    if(movingTile!=-1){
                int newPosition=player.moveTileOnBoard(movingTile, ((TileActor)getActor()).getPosition());

                    }
                }
            });

1 个答案:

答案 0 :(得分:0)

问题是您向Actor提供了自己的Payload(因此DragAndDrop请注意删除此演员):

payload.setDragActor(getActor());

尝试创建Actor的副本:

Actor newActor = /* clone your actor from getActor() to newActor */;
payload.setDragActor(newActor);