gpt4 book ai didi

java - 循环遍历tiles,只有get才能获取第一个tile的索引

转载 作者:行者123 更新时间:2023-12-01 12:09:20 25 4
gpt4 key购买 nike

我创建了一个非常基本的图 block 系统,在其中我希望能够获取鼠标悬停在其上的当前图 block ,这对于创建的第一个图 block 非常有用,但除此之外我什么也没看。

这是设置和绘制图 block 的代码以及“当前悬停”文本。

private static final int TILE_WIDTH = 64;
private static final int TILE_HEIGHT = 64;

private static Tile[][] gridTiles = new Tile[(int)Math.ceil(Gdx.graphics.getHeight() / TILE_HEIGHT)][(int)Math.ceil(Gdx.graphics.getWidth() / TILE_WIDTH)];

public CombatGrid() {} // Cannot instantiate this class

static {
// Rows
for(int r = 0; r < gridTiles.length; r++) {
// Columns
for(int c = 0; c < gridTiles[0].length; c++) {
gridTiles[r][c] = new Tile((c * TILE_WIDTH), (r * TILE_HEIGHT), TILE_WIDTH, TILE_HEIGHT);
System.out.println("New tile("+r+","+c+") was created at: x: " + (c * TILE_WIDTH) + " y: " + (r * TILE_HEIGHT));
}
}
}

public static void draw(Batch batch) {
for(int r = 0; r < gridTiles.length; r++) {
for(int c = 0; c < gridTiles[0].length; c++) {
gridTiles[r][c].draw(batch);
if(gridTiles[r][c].hovering(Gdx.input.getX(), Gdx.input.getY())) {
Assets.defaultFont.draw(batch, "Current hover index("+r+","+c+")", Client.TOP_LEFT_X + 10, Client.TOP_LEFT_Y - 30);
}
}
}
}

文本仅在我越过 Tile[0][0] 时显示

这是 Tile 类的悬停方法

public boolean hovering(float x, float y) {
if((x > this.x) && (x < this.width)) {
if((y > this.y) && (y < this.height)) {
return true;
}
}
return false;
}

我传递给悬停的鼠标输入也是正确的,当我移出 0,0 block 边界时,它不再绘制。

另外,我正在调试我的鼠标位置

Assets.defaultFont.draw(batch, "Curret mouse X: " + Gdx.input.getX() + " Y: " + Gdx.input.getY(), Client.TOP_LEFT_X + 10, Client.TOP_LEFT_Y - 10);

它表明我设置的数学是正确的,或者至少应该是正确的,除非我错过了一些东西

现在,我知道所有图 block 都已被放大并正确放置,因为静态初始化程序中的调试语句显示:

New tile(0,0) was created at: x: 0 y: 0
New tile(0,1) was created at: x: 64 y: 0
New tile(0,2) was created at: x: 128 y: 0
New tile(0,3) was created at: x: 192 y: 0
New tile(0,4) was created at: x: 256 y: 0
New tile(0,5) was created at: x: 320 y: 0
New tile(0,6) was created at: x: 384 y: 0
New tile(0,7) was created at: x: 448 y: 0
New tile(0,8) was created at: x: 512 y: 0
New tile(0,9) was created at: x: 576 y: 0
New tile(0,10) was created at: x: 640 y: 0
New tile(0,11) was created at: x: 704 y: 0
New tile(0,12) was created at: x: 768 y: 0
New tile(0,13) was created at: x: 832 y: 0
New tile(0,14) was created at: x: 896 y: 0
New tile(0,15) was created at: x: 960 y: 0
New tile(0,16) was created at: x: 1024 y: 0
New tile(0,17) was created at: x: 1088 y: 0
New tile(1,0) was created at: x: 0 y: 64
New tile(1,1) was created at: x: 64 y: 64
New tile(1,2) was created at: x: 128 y: 64
New tile(1,3) was created at: x: 192 y: 64
New tile(1,4) was created at: x: 256 y: 64
New tile(1,5) was created at: x: 320 y: 64
New tile(1,6) was created at: x: 384 y: 64
New tile(1,7) was created at: x: 448 y: 64
New tile(1,8) was created at: x: 512 y: 64
New tile(1,9) was created at: x: 576 y: 64
New tile(1,10) was created at: x: 640 y: 64
New tile(1,11) was created at: x: 704 y: 64
New tile(1,12) was created at: x: 768 y: 64
New tile(1,13) was created at: x: 832 y: 64
New tile(1,14) was created at: x: 896 y: 64
New tile(1,15) was created at: x: 960 y: 64
New tile(1,16) was created at: x: 1024 y: 64
New tile(1,17) was created at: x: 1088 y: 64
New tile(2,0) was created at: x: 0 y: 128
New tile(2,1) was created at: x: 64 y: 128
New tile(2,2) was created at: x: 128 y: 128
New tile(2,3) was created at: x: 192 y: 128
New tile(2,4) was created at: x: 256 y: 128
New tile(2,5) was created at: x: 320 y: 128
New tile(2,6) was created at: x: 384 y: 128
New tile(2,7) was created at: x: 448 y: 128
New tile(2,8) was created at: x: 512 y: 128
New tile(2,9) was created at: x: 576 y: 128
New tile(2,10) was created at: x: 640 y: 128
New tile(2,11) was created at: x: 704 y: 128
New tile(2,12) was created at: x: 768 y: 128
New tile(2,13) was created at: x: 832 y: 128
New tile(2,14) was created at: x: 896 y: 128
New tile(2,15) was created at: x: 960 y: 128
New tile(2,16) was created at: x: 1024 y: 128
New tile(2,17) was created at: x: 1088 y: 128
New tile(3,0) was created at: x: 0 y: 192
New tile(3,1) was created at: x: 64 y: 192
New tile(3,2) was created at: x: 128 y: 192
New tile(3,3) was created at: x: 192 y: 192
New tile(3,4) was created at: x: 256 y: 192
New tile(3,5) was created at: x: 320 y: 192
New tile(3,6) was created at: x: 384 y: 192
New tile(3,7) was created at: x: 448 y: 192
New tile(3,8) was created at: x: 512 y: 192
New tile(3,9) was created at: x: 576 y: 192
New tile(3,10) was created at: x: 640 y: 192
New tile(3,11) was created at: x: 704 y: 192
New tile(3,12) was created at: x: 768 y: 192
New tile(3,13) was created at: x: 832 y: 192
New tile(3,14) was created at: x: 896 y: 192
New tile(3,15) was created at: x: 960 y: 192
New tile(3,16) was created at: x: 1024 y: 192
New tile(3,17) was created at: x: 1088 y: 192
New tile(4,0) was created at: x: 0 y: 256
New tile(4,1) was created at: x: 64 y: 256
New tile(4,2) was created at: x: 128 y: 256
New tile(4,3) was created at: x: 192 y: 256
New tile(4,4) was created at: x: 256 y: 256
New tile(4,5) was created at: x: 320 y: 256
New tile(4,6) was created at: x: 384 y: 256
New tile(4,7) was created at: x: 448 y: 256
New tile(4,8) was created at: x: 512 y: 256
New tile(4,9) was created at: x: 576 y: 256
New tile(4,10) was created at: x: 640 y: 256
New tile(4,11) was created at: x: 704 y: 256
New tile(4,12) was created at: x: 768 y: 256
New tile(4,13) was created at: x: 832 y: 256
New tile(4,14) was created at: x: 896 y: 256
New tile(4,15) was created at: x: 960 y: 256
New tile(4,16) was created at: x: 1024 y: 256
New tile(4,17) was created at: x: 1088 y: 256
New tile(5,0) was created at: x: 0 y: 320
New tile(5,1) was created at: x: 64 y: 320
New tile(5,2) was created at: x: 128 y: 320
New tile(5,3) was created at: x: 192 y: 320
New tile(5,4) was created at: x: 256 y: 320
New tile(5,5) was created at: x: 320 y: 320
New tile(5,6) was created at: x: 384 y: 320
New tile(5,7) was created at: x: 448 y: 320
New tile(5,8) was created at: x: 512 y: 320
New tile(5,9) was created at: x: 576 y: 320
New tile(5,10) was created at: x: 640 y: 320
New tile(5,11) was created at: x: 704 y: 320
New tile(5,12) was created at: x: 768 y: 320
New tile(5,13) was created at: x: 832 y: 320
New tile(5,14) was created at: x: 896 y: 320
New tile(5,15) was created at: x: 960 y: 320
New tile(5,16) was created at: x: 1024 y: 320
New tile(5,17) was created at: x: 1088 y: 320
New tile(6,0) was created at: x: 0 y: 384
New tile(6,1) was created at: x: 64 y: 384
New tile(6,2) was created at: x: 128 y: 384
New tile(6,3) was created at: x: 192 y: 384
New tile(6,4) was created at: x: 256 y: 384
New tile(6,5) was created at: x: 320 y: 384
New tile(6,6) was created at: x: 384 y: 384
New tile(6,7) was created at: x: 448 y: 384
New tile(6,8) was created at: x: 512 y: 384
New tile(6,9) was created at: x: 576 y: 384
New tile(6,10) was created at: x: 640 y: 384
New tile(6,11) was created at: x: 704 y: 384
New tile(6,12) was created at: x: 768 y: 384
New tile(6,13) was created at: x: 832 y: 384
New tile(6,14) was created at: x: 896 y: 384
New tile(6,15) was created at: x: 960 y: 384
New tile(6,16) was created at: x: 1024 y: 384
New tile(6,17) was created at: x: 1088 y: 384
New tile(7,0) was created at: x: 0 y: 448
New tile(7,1) was created at: x: 64 y: 448
New tile(7,2) was created at: x: 128 y: 448
New tile(7,3) was created at: x: 192 y: 448
New tile(7,4) was created at: x: 256 y: 448
New tile(7,5) was created at: x: 320 y: 448
New tile(7,6) was created at: x: 384 y: 448
New tile(7,7) was created at: x: 448 y: 448
New tile(7,8) was created at: x: 512 y: 448
New tile(7,9) was created at: x: 576 y: 448
New tile(7,10) was created at: x: 640 y: 448
New tile(7,11) was created at: x: 704 y: 448
New tile(7,12) was created at: x: 768 y: 448
New tile(7,13) was created at: x: 832 y: 448
New tile(7,14) was created at: x: 896 y: 448
New tile(7,15) was created at: x: 960 y: 448
New tile(7,16) was created at: x: 1024 y: 448
New tile(7,17) was created at: x: 1088 y: 448
New tile(8,0) was created at: x: 0 y: 512
New tile(8,1) was created at: x: 64 y: 512
New tile(8,2) was created at: x: 128 y: 512
New tile(8,3) was created at: x: 192 y: 512
New tile(8,4) was created at: x: 256 y: 512
New tile(8,5) was created at: x: 320 y: 512
New tile(8,6) was created at: x: 384 y: 512
New tile(8,7) was created at: x: 448 y: 512
New tile(8,8) was created at: x: 512 y: 512
New tile(8,9) was created at: x: 576 y: 512
New tile(8,10) was created at: x: 640 y: 512
New tile(8,11) was created at: x: 704 y: 512
New tile(8,12) was created at: x: 768 y: 512
New tile(8,13) was created at: x: 832 y: 512
New tile(8,14) was created at: x: 896 y: 512
New tile(8,15) was created at: x: 960 y: 512
New tile(8,16) was created at: x: 1024 y: 512
New tile(8,17) was created at: x: 1088 y: 512
New tile(9,0) was created at: x: 0 y: 576
New tile(9,1) was created at: x: 64 y: 576
New tile(9,2) was created at: x: 128 y: 576
New tile(9,3) was created at: x: 192 y: 576
New tile(9,4) was created at: x: 256 y: 576
New tile(9,5) was created at: x: 320 y: 576
New tile(9,6) was created at: x: 384 y: 576
New tile(9,7) was created at: x: 448 y: 576
New tile(9,8) was created at: x: 512 y: 576
New tile(9,9) was created at: x: 576 y: 576
New tile(9,10) was created at: x: 640 y: 576
New tile(9,11) was created at: x: 704 y: 576
New tile(9,12) was created at: x: 768 y: 576
New tile(9,13) was created at: x: 832 y: 576
New tile(9,14) was created at: x: 896 y: 576
New tile(9,15) was created at: x: 960 y: 576
New tile(9,16) was created at: x: 1024 y: 576
New tile(9,17) was created at: x: 1088 y: 576
New tile(10,0) was created at: x: 0 y: 640
New tile(10,1) was created at: x: 64 y: 640
New tile(10,2) was created at: x: 128 y: 640
New tile(10,3) was created at: x: 192 y: 640
New tile(10,4) was created at: x: 256 y: 640
New tile(10,5) was created at: x: 320 y: 640
New tile(10,6) was created at: x: 384 y: 640
New tile(10,7) was created at: x: 448 y: 640
New tile(10,8) was created at: x: 512 y: 640
New tile(10,9) was created at: x: 576 y: 640
New tile(10,10) was created at: x: 640 y: 640
New tile(10,11) was created at: x: 704 y: 640
New tile(10,12) was created at: x: 768 y: 640
New tile(10,13) was created at: x: 832 y: 640
New tile(10,14) was created at: x: 896 y: 640
New tile(10,15) was created at: x: 960 y: 640
New tile(10,16) was created at: x: 1024 y: 640
New tile(10,17) was created at: x: 1088 y: 640
New tile(11,0) was created at: x: 0 y: 704
New tile(11,1) was created at: x: 64 y: 704
New tile(11,2) was created at: x: 128 y: 704
New tile(11,3) was created at: x: 192 y: 704
New tile(11,4) was created at: x: 256 y: 704
New tile(11,5) was created at: x: 320 y: 704
New tile(11,6) was created at: x: 384 y: 704
New tile(11,7) was created at: x: 448 y: 704
New tile(11,8) was created at: x: 512 y: 704
New tile(11,9) was created at: x: 576 y: 704
New tile(11,10) was created at: x: 640 y: 704
New tile(11,11) was created at: x: 704 y: 704
New tile(11,12) was created at: x: 768 y: 704
New tile(11,13) was created at: x: 832 y: 704
New tile(11,14) was created at: x: 896 y: 704
New tile(11,15) was created at: x: 960 y: 704
New tile(11,16) was created at: x: 1024 y: 704
New tile(11,17) was created at: x: 1088 y: 704

最佳答案

试试这个;

public boolean hovering(float x, float y) {
if((x > this.x) && (x < this.x + this.width)) {
if((y > this.y) && (y < this.y + this.height)) {
return true;
}
}
return false;
}

关于java - 循环遍历tiles,只有get才能获取第一个tile的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27350072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com