Libgdx 修正拖拽逻辑

Libgdx 修正拖拽逻辑,libgdx,Libgdx,我想要什么: 我想允许用户拖放他放在屏幕上的演员 1) 一个演员(32x32像素)可以放在我定义的某个区域(一个中心矩形5x5,每个单元格32像素) 2) 当演员被添加到舞台上时,他们会被捕捉到一个想象中的屏幕网格上 3) 拖放过程中也需要捕捉到栅格 4) 一个演员不能被另一个演员感动 5) 当用户单击某个角色时,该角色必须旋转90度 6) 演员不能被移动到边界之外 我实际得到的: 1) 嗯 2) 嗯 3) ok(有bug) 4) 尚未实施 5) ok(有bug) 6) 尚未实施 错误是: 1)

我想要什么:

我想允许用户拖放他放在屏幕上的演员

1) 一个演员(32x32像素)可以放在我定义的某个区域(一个中心矩形5x5,每个单元格32像素)

2) 当演员被添加到舞台上时,他们会被捕捉到一个想象中的屏幕网格上

3) 拖放过程中也需要捕捉到栅格

4) 一个演员不能被另一个演员感动

5) 当用户单击某个角色时,该角色必须旋转90度

6) 演员不能被移动到边界之外

我实际得到的:

1) 嗯 2) 嗯 3) ok(有bug) 4) 尚未实施 5) ok(有bug) 6) 尚未实施

错误是:

1) 旋转在任何情况下都适用。我不想要这种行为。如果用户只是做拖放操作,我不想旋转演员。(我知道会发生这种情况,因为我将这部分代码放在了触地方法中)

2) 这很难用我的英语来形容,我会努力的。 我放了一个演员,我可以移动它(当我说“移动”时,我指的是拖放)。 我放了第二个演员,我可以移动它。 现在,如果我尝试移动我放置的第一个演员,第二个演员会移动到第一个演员的上方(与第一个演员重叠),并开始跟随第一个演员移动

现在代码是:

游戏阶段的事件

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    translateScreenToWorldCoordinates(screenX, screenY);

    // check if the user clicked or not in the center area
    if(!centerArea.contains(touchPoint.x, touchPoint.y)) {
        return true;
    }

    // rotation
    Iterator<Square> squareIterator = squares.iterator();
    while(squareIterator.hasNext()) {
        Square nextSquare = squareIterator.next();
        if(nextSquare.getBounds().contains(touchPoint.x, touchPoint.y)) {
            nextSquare.rotate();
            return true;
        }
    }

    // add an Actor

    touchPoint.set(touchPoint.x - Constants.SQUARE_SIZE / 2, touchPoint.y - Constants.SQUARE_SIZE / 2, 0f);

    float snapX = Math.round((touchPoint.x / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
    float snapY = Math.round((touchPoint.y / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;

    Square square = new Square(snapX, snapY);
    squares.add(square);
    addActor(square);

    return true;
}

public boolean touchDragged(int screenX, int screenY, int pointer) {

    Vector3 newTouchPoint = new Vector3(screenX, screenY, 0f);
    getCamera().unproject(newTouchPoint.set(screenX, screenY, 0f));

    Square next = null;

    Iterator<Square> squareIterator = squares.listIterator();
    while(squareIterator.hasNext()) {
        next = squareIterator.next();

        if(next.getBounds().contains(newTouchPoint.x, newTouchPoint.y)) {

            next.setX(newTouchPoint.x - Constants.SQUARE_SIZE / 2);
            next.setY(newTouchPoint.y - Constants.SQUARE_SIZE / 2);

            continue;
        }
    }

    newTouchPoint.set(newTouchPoint.x - Constants.SQUARE_SIZE / 2, newTouchPoint.y - Constants.SQUARE_SIZE / 2, 0f);
    float snapX = Math.round((newTouchPoint.x / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
    float snapY = Math.round((newTouchPoint.y / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
    next.setX(snapX);
    next.setY(snapY);

    return true;
}

private void translateScreenToWorldCoordinates(int x, int y) {
    getCamera().unproject(touchPoint.set(x, y, 0f));
}
@覆盖
公共布尔接地(整数屏幕X、整数屏幕Y、整数指针、整数按钮){
平移屏幕世界坐标(屏幕X、屏幕Y);
//检查用户是否单击了中心区域
如果(!centera.contains(touchPoint.x,touchPoint.y)){
返回true;
}
//轮换
迭代器squareIterator=squares.Iterator();
while(squareIterator.hasNext()){
Square-nextSquare=squareIterator.next();
if(nextSquare.getBounds().contains(touchPoint.x,touchPoint.y)){
nextSquare.rotate();
返回true;
}
}
//加演员
设置(touchPoint.x-Constants.SQUARE\u SIZE/2,touchPoint.y-Constants.SQUARE\u SIZE/2,0f);
float snapX=Math.round((touchPoint.x/Constants.SQUARE\u SIZE))*Constants.SQUARE\u SIZE;
float snapY=Math.round((touchPoint.y/Constants.SQUARE\u SIZE))*Constants.SQUARE\u SIZE;
正方形=新正方形(snapX,snapY);
正方形。添加(正方形);
加法器(正方形);
返回true;
}
公共布尔值(int-screenX、int-screenY、int-pointer){
Vector3新触点=新的Vector3(屏幕X,屏幕Y,0f);
getCamera().unproject(newTouchPoint.set(screenX,screenY,0f));
下一个平方=空;
迭代器squareIterator=squares.listIterator();
while(squareIterator.hasNext()){
next=squareIterator.next();
if(next.getBounds()包含(newTouchPoint.x,newTouchPoint.y)){
setX(newTouchPoint.x-Constants.SQUARE_SIZE/2);
next.setY(newTouchPoint.y-Constants.SQUARE_SIZE/2);
继续;
}
}
newTouchPoint.set(newTouchPoint.x-Constants.SQUARE_SIZE/2,newTouchPoint.y-Constants.SQUARE_SIZE/2,0f);
float snapX=Math.round((newTouchPoint.x/Constants.SQUARE\u SIZE))*Constants.SQUARE\u SIZE;
float snapY=Math.round((newTouchPoint.y/Constants.SQUARE\u SIZE))*Constants.SQUARE\u SIZE;
next.setX(snapX);
接下来是赛蒂(斯内普);
返回true;
}
私有void translateScreentWorld坐标(int x,int y){
getCamera().unproject(touchPoint.set(x,y,0f));
}
演员

public class Square extends GameActor {

    private float rotation;
    private ShapeRenderer shapeRenderer;

    public Square(float x, float y) {
        super(x, y);
        shapeRenderer = new ShapeRenderer();
    }

    @Override
    protected void initializeSprite() {
        texture = new Texture(Gdx.files.internal(Constants.SQUARE_LOCATION));
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    protected void updateBounds() {
        bounds.setPosition(x, y);
    }

    public void rotate() {
        rotation -= 90;
        if(rotation <= -360) {
            rotation = 0;
        }
        setRotation(rotation);
    }

    public void setX(float x) {
        this.x = x;
    }

    public void setY(float y) {
        this.y = y;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        /*shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        shapeRenderer.setColor(Color.RED);
        shapeRenderer.rect(bounds.getX(), bounds.getY(), Constants.SQUARE_SIZE, Constants.SQUARE_SIZE);
        shapeRenderer.end();*/
        //batch.begin();

        batch.draw(texture, x, y, texture.getWidth() / 2, texture.getHeight() / 2,
            texture.getWidth(), texture.getHeight(), 1, 1, rotation, 0, 0, Constants.SQUARE_SIZE, Constants.SQUARE_SIZE, false, false);
    }
}
公共类Square扩展GameActor{
私人浮动轮换;
私人shaperender shaperender;
公共广场(浮标x、浮标y){
super(x,y);
ShaperEnder=新的ShaperEnder();
}
@凌驾
受保护的void initializeSprite(){
纹理=新纹理(Gdx.files.internal(Constants.SQUARE_LOCATION));
}
@凌驾
公共无效法案(浮动三角洲){
超级法案(德尔塔);
}
@凌驾
受保护的void updateBounds(){
边界。设置位置(x,y);
}
公共空间(英文){
旋转-=90;

如果(旋转而不是重新发明轮子,为什么不使用
libgdx
?可以找到一个关于这个的很棒的视频教程。我已经看过那个视频了。实际上我没有使用皮肤、表格等等。所以我不知道如何在我的代码中实现它。我想修复我的代码,然后尝试改进它。而且,我的实现似乎更容易。拖放功能并不特定于皮肤或表格的使用。在我看来,值得学习如何在您的特定情况下使用此功能,因为它已经过测试并被许多其他用户使用。从长远来看,它将节省您的时间和精力。例如,在视频中,他已经在库存中有一些项目,可以用来设置e source和所有东西。在我的代码中,一开始,我没有演员。当用户用鼠标单击时,会添加一个演员。然后,可以移动该演员。当创建
演员时,您可以创建目标的
源代码。然后使用
添加按钮将
源代码添加到
DragAndDrop
对象中ce
方法。