Libgdx拖放,如何同步光标和sprite/actor?
Libgdx Drag And Drop, how to sync cursor and sprite/actor?
Gdx 版本:1.9.8
你好,我正在创建 JRPG,我有一个带有拖放系统的库存,问题是,当我点击项目时,它的位置在远离光标的左上角(见屏幕截图),如何制作精灵位于光标的中心(通常应该如何)???
SCREENSHOT!!!How it looks like
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload;
import com.redsoft.redrune.InventoryItem;
public class InventorySlotTarget extends Target {
InventorySlot _targetSlot;
public InventorySlotTarget(InventorySlot actor) {
super(actor);
_targetSlot = actor;
}
@Override
public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
return true;
}
@Override
public void reset(Source source, Payload payload) {
}
@Override
public void drop(Source source, Payload payload, float x, float y, int pointer) {
InventoryItem sourceActor = (InventoryItem) payload.getDragActor();
InventoryItem targetActor = _targetSlot.getTopInventoryItem();
InventorySlot sourceSlot = ((InventorySlotSource) source).getSourceSlot();
if (sourceActor == null) {
return;
}
//First, does the slot accept the source item type?
if (!_targetSlot.doesAcceptItemUseType(sourceActor.getItemUseType())) {
//Put item back where it came from, slot doesn't accept item
sourceSlot.add(sourceActor);
return;
}
if (!_targetSlot.hasItem()) {
_targetSlot.add(sourceActor);
} else {
//If the same item and stackable, add
if (sourceActor.isSameItemType(targetActor) && sourceActor.isStackable()) {
_targetSlot.add(sourceActor);
} else {
//If they aren't the same items or the items aren't stackable, then swap
InventorySlot.swapSlots(sourceSlot, _targetSlot, sourceActor);
}
}
}
}
更新!
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target;
public class InventorySlotSource extends Source {
private DragAndDrop _dragAndDrop;
private InventorySlot _sourceSlot;
public InventorySlotSource(InventorySlot sourceSlot, DragAndDrop dragAndDrop) {
super(sourceSlot.getTopInventoryItem());
this._sourceSlot = sourceSlot;
this._dragAndDrop = dragAndDrop;
}
@Override
public Payload dragStart(InputEvent event, float x, float y, int pointer) {
Payload payload = new Payload();
Actor actor = getActor();
if (actor == null) {
return null;
}
InventorySlot source = (InventorySlot) actor.getParent();
if (source == null) {
return null;
} else {
_sourceSlot = source;
}
_sourceSlot.decrementItemCount(true);
payload.setDragActor(getActor());
_dragAndDrop.setDragActorPosition(-x, -y + getActor().getHeight());
return payload;
}
@Override
public void dragStop(InputEvent event, float x, float y, int pointer, Payload payload, Target target) {
if (target == null) {
_sourceSlot.add(payload.getDragActor());
}
}
public InventorySlot getSourceSlot() {
return _sourceSlot;
}
}
这是在 DragAndDrop 对象中设置的。默认位置假定您希望演员的右下角与光标对齐。你可以这样居中:
dragAndDrop.setDragActorPosition(dragActor.getWidth() / 2, -dragActor.getHeight() / 2);
你只需要在设置拖动角色后调用一次。我认为方法名称具有误导性(应该说偏移量而不是位置)。
Gdx 版本:1.9.8
你好,我正在创建 JRPG,我有一个带有拖放系统的库存,问题是,当我点击项目时,它的位置在远离光标的左上角(见屏幕截图),如何制作精灵位于光标的中心(通常应该如何)???
SCREENSHOT!!!How it looks like
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload;
import com.redsoft.redrune.InventoryItem;
public class InventorySlotTarget extends Target {
InventorySlot _targetSlot;
public InventorySlotTarget(InventorySlot actor) {
super(actor);
_targetSlot = actor;
}
@Override
public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
return true;
}
@Override
public void reset(Source source, Payload payload) {
}
@Override
public void drop(Source source, Payload payload, float x, float y, int pointer) {
InventoryItem sourceActor = (InventoryItem) payload.getDragActor();
InventoryItem targetActor = _targetSlot.getTopInventoryItem();
InventorySlot sourceSlot = ((InventorySlotSource) source).getSourceSlot();
if (sourceActor == null) {
return;
}
//First, does the slot accept the source item type?
if (!_targetSlot.doesAcceptItemUseType(sourceActor.getItemUseType())) {
//Put item back where it came from, slot doesn't accept item
sourceSlot.add(sourceActor);
return;
}
if (!_targetSlot.hasItem()) {
_targetSlot.add(sourceActor);
} else {
//If the same item and stackable, add
if (sourceActor.isSameItemType(targetActor) && sourceActor.isStackable()) {
_targetSlot.add(sourceActor);
} else {
//If they aren't the same items or the items aren't stackable, then swap
InventorySlot.swapSlots(sourceSlot, _targetSlot, sourceActor);
}
}
}
}
更新!
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target;
public class InventorySlotSource extends Source {
private DragAndDrop _dragAndDrop;
private InventorySlot _sourceSlot;
public InventorySlotSource(InventorySlot sourceSlot, DragAndDrop dragAndDrop) {
super(sourceSlot.getTopInventoryItem());
this._sourceSlot = sourceSlot;
this._dragAndDrop = dragAndDrop;
}
@Override
public Payload dragStart(InputEvent event, float x, float y, int pointer) {
Payload payload = new Payload();
Actor actor = getActor();
if (actor == null) {
return null;
}
InventorySlot source = (InventorySlot) actor.getParent();
if (source == null) {
return null;
} else {
_sourceSlot = source;
}
_sourceSlot.decrementItemCount(true);
payload.setDragActor(getActor());
_dragAndDrop.setDragActorPosition(-x, -y + getActor().getHeight());
return payload;
}
@Override
public void dragStop(InputEvent event, float x, float y, int pointer, Payload payload, Target target) {
if (target == null) {
_sourceSlot.add(payload.getDragActor());
}
}
public InventorySlot getSourceSlot() {
return _sourceSlot;
}
}
这是在 DragAndDrop 对象中设置的。默认位置假定您希望演员的右下角与光标对齐。你可以这样居中:
dragAndDrop.setDragActorPosition(dragActor.getWidth() / 2, -dragActor.getHeight() / 2);
你只需要在设置拖动角色后调用一次。我认为方法名称具有误导性(应该说偏移量而不是位置)。