【问题标题】:Libgdx Drag And Drop, how to sync cursor and sprite/actor?Libgdx拖放,如何同步光标和精灵/演员?
【发布时间】:2018-03-22 09:23:17
【问题描述】:

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

【问题讨论】:

    标签: java user-interface libgdx drag-and-drop inventory


    【解决方案1】:

    这是在 DragAndDrop 对象中设置的。默认位置假定您希望演员的右下角与光标对齐。你可以这样居中:

    dragAndDrop.setDragActorPosition(dragActor.getWidth() / 2, -dragActor.getHeight() / 2);
    

    您只需在设置拖动 actor 后调用一次。我认为方法名称具有误导性(应该说偏移而不是位置)。

    【讨论】:

    • public boolean drag(Source source, Payload payload, float x, float y, int pointer) { DragAndDrop dragAndDrop = new DragAndDrop(); dragAndDrop.setDragActorPosition(dragAndDrop.getWidth() / 2, -dragAndDrop.getHeight() / 2);返回真; }
    • getWidth 和 getHeight 红色突出显示并显示“无法解析方法”。
    • @RedasShuliakas 你不能随机生成其他 DragAndDrop。对您已经拥有的管理此目标项目的人执行此操作。此外,应该在拖动 actor 上调用 getWidth(),但您在错误的对象上调用它。
    • 您没有在设置您正在使用的 DragAndDrop 或设置拖动演员的位置共享您的代码。设置拖动演员后,您只需将我的代码行放在上面即可。
    • 你已经有_dragAndDrop.setDragActorPosition(-x, -y + getActor().getHeight());这行了。将其更改为_dragAndDrop.setDragActorPosition(getActor().getWidth() / 2, -getActor().getHeight() / 2);。看看我上面的第一条评论。 getWidth() 是在 actor 上调用的,而不是 DragAndDrop。
    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多