简体   繁体   English

为什么此Java网格无法正常运行?

[英]Why does this Java grid not function correctly?

I am trying to make a grid (with minimal code) that blocks can snap to. 我正在尝试制作一个块可以捕捉到的网格(使用最少的代码)。 What I want is when the mouse is in the grid square, the block moves to that square. 我想要的是,当鼠标在网格正方形中时,块将移动到该正方形。 What I have written essentially says that if the X or Y are beyond the grid block times the size of the block, move to the next grid block. 我写的基本上是说,如果X或Y超出网格块乘以块大小,则移至下一个网格块。 Currently, this code creates a 3x3 grid, though the code SHOULD generate infinite gridspaces. 目前,此代码将创建3x3网格,尽管该代码应生成无限的网格空间。 I cannot move the block outside of this 3x3 grid. 我无法将块移动到此3x3网格之外。

public class Player extends Entity {
public Player(double entSize, boolean collideA, boolean collideB, double x, double y) {
    super(entSize, collideA, collideB, x, y);
}
public void init() {
    Texture texFile = loadTexture("stone");
    texture(false, true);
    texFile.bind();
    render();
}
void input() {
    int gridPosX = 1;
    int gridPosY = 1;
        if(getX() > gridPosX*entSize) {
            gridPosX += 1;
        } if(getX() < gridPosX*entSize) {
            gridPosX -= 1;
        } if(getY() > gridPosY*entSize) {
            gridPosY += 1;
        } if(getY() < gridPosY*entSize) {
            gridPosY -= 1;
        }
        this.x = gridPosX*entSize;
        this.y = gridPosY*entSize;
}

} The X and Y values are what define the blocks position and shape parameters. } X和Y值定义块的位置和形状参数。

Fixed by using for loops for the positive grid increments! 使用正循环增量的for循环进行修复! NEW CODE: 新代码:

public class Player extends Entity {
public Player(double entSize, boolean collideA, boolean collideB, double x, double y) {
    super(entSize, collideA, collideB, x, y);
}
public void init() {
    Texture texFile = loadTexture("stone");
    texture(false, true);
    texFile.bind();
    render();
}
void input() {
    int gridPosX = 0;
    int gridPosY = 0;
        for(gridPosX = 0; getX() > gridPosX*entSize; gridPosX++) {
            gridPosX += 0;
        } if(getX() < gridPosX*entSize) {
            gridPosX -= 1;
        } for(gridPosY = 0; getY() > gridPosY*entSize; gridPosY++) {
            gridPosY += 0;
        } if(getY() < gridPosY*entSize) {
            gridPosY -= 1;
        }
        this.x = gridPosX*entSize;
        this.y = gridPosY*entSize;
}

} }

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

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