我正在开发一个rummikub游戏,我的棋盘上有瓷砖,并且板上的所有位置都定义为TileActor。一些平铺演员上有实际的平铺,有些是空的。问题是:当我将图块拖放到空的TileActor上时,它工作正常,但是当我将其放在空白空间上时,actor迷路了。如何防止演员迷路?

这是我的拖放代码:

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

                    }
                }
            });

最佳答案

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

payload.setDragActor(getActor());


尝试创建Actor的副本:

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

08-04 22:34