我已经实现了拖放操作,但问题出在用户释放拖动之后。被拖走的演员从屏幕上消失了。如何将actor移动到新的位置,或者如果它放在不适当的位置,则返回到它的位置?
感谢您的回答。
此致
阿尔法 “
final DragAndDrop dragAndDrop = new DragAndDrop();
final Box sourceBox = boxList.get(0);
final Box targetBox = boxHolderList.get(0);
Source source = new Source(sourceBox) {
@Override
public void dragStop(InputEvent event, float x, float y,
int pointer, Payload payload, Target target) {
Box sourceBox = (Box) payload.getDragActor();
if (target == null){
sourceBox.setPosition(sourceBox.getX(), sourceBox.getY());
}
else{
Box targetBox = (Box) target.getActor();
sourceBox.setPosition(targetBox.getX(), targetBox.getY());
}
}
@Override
public Payload dragStart(InputEvent event, float x, float y, int pointer) {
dragAndDrop.setDragActorPosition(-1*sourceBox.getWidth()/2, 1*sourceBox.getHeight()/2);
Payload payload = new Payload();
payload.setDragActor(sourceBox);
Label validLabel = new Label("Valid!", 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;
}
};
答案 0 :(得分:0)
这种拖放方法很网状,你可以试试这个
yourActor.addListener(new DragListener(){
public void touchDragged(InputEvent event, float x, float y, int pointer) {
yourActor.moveBy(x-yourActor.getWidth()/2,y-yourActor.getHeight()/2);
}});