简体   繁体   English

Libgdx拖放,如何同步光标和精灵/演员?

[英]Libgdx Drag And Drop, how to sync cursor and sprite/actor?

Gdx version: 1.9.8 Gdx版本:1.9.8

Hello, I am creating JRPG and I have a Inventory with drag and drop systems, problem is, when I click on item, position of it on left up corner far from cursor (see on screenshot), how to make sprite to be in center of the cursor (how it's normally should be)??? 您好,我正在创建JRPG,并且我有一个带有拖放系统的清单,问题是,当我单击项目时,它的位置在远离光标的左上角(请参见屏幕截图),如何使精灵位于中间的光标(通常应该是)???

SCREENSHOT!!!How it looks like SCREENSHOT !!!看起来如何

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

    }
}

UPDATE! 更新!

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

This is set in the DragAndDrop object. 这在DragAndDrop对象中设置。 The default position assumes you want the bottom right corner of the actor to align with the cursor. 默认位置假定您希望角色的右下角与光标对齐。 You can center it like this: 您可以像这样居中:

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

You only have to call this once after setting the drag actor. 设置拖动actor后,只需调用一次即可。 I think the method name is misleading (it should say offset instead of position). 我认为方法名称具有误导性(应该说偏移而不是位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM