我只是一个初学者,所以这里可能是一些我可以识别的小错误但是...我正在使用libGDX库和我的方法的代码:
public Vector2[] initCollisionStart() {
int i = 0;
for (int row = 0; row < currentMapHeightTiles; row++) {
for (int col = 0; col < 9; col++) {
if (currentMap[row][col] == 4) {
i++;
} else if (currentMap[row][col] == 3) {
i++;
}
}
}
Gdx.app.log("initCollisionStart", "works");
Vector2[] collisionStart = new Vector2[i];
for (int row = 0; row < currentMapHeightTiles; row++) {
for (int col = 0; col < 9; col++) {
if (currentMap[row][col] == 4) {
position.x = col * 40;
position.y = row * 40;
collisionStart[i - 1] = position;
System.out.println("CollisionStart[" + i + "] = " + collisionStart[i - 1]);
i--;
} else if (currentMap[row][col] == 3) {
position.x = col * 40;
position.y = row * 40;
collisionStart[i - 1] = position;
System.out.println("CollisionStart[" + i + "] = " + collisionStart[i - 1]);
i--;
}
}
}
collisionInitialized = true;
System.out.print("\n");
for (int z = 0; z < collisionStart.length; z++) {
System.out.println("CollisionStart[" + z + "] = " + collisionStart[z]);
}
return collisionStart;
}
我的输出看起来像是:
initCollisionStart: works
CollisionStart[7] = [40.0:5760.0]
CollisionStart[6] = [80.0:5760.0]
CollisionStart[5] = [120.0:5760.0]
CollisionStart[4] = [160.0:5760.0]
CollisionStart[3] = [200.0:5760.0]
CollisionStart[2] = [240.0:5760.0]
CollisionStart[1] = [280.0:5760.0]
CollisionStart[0] = [280.0:5760.0]
CollisionStart[1] = [280.0:5760.0]
CollisionStart[2] = [280.0:5760.0]
CollisionStart[3] = [280.0:5760.0]
CollisionStart[4] = [280.0:5760.0]
CollisionStart[5] = [280.0:5760.0]
CollisionStart[6] = [280.0:5760.0]
这是我的问题。当我在这个双重for循环之外使用它时,为什么这个collisionStart数组的每个值都等于[280.0:5760.0]?我希望它返回之前初始化的相同值。有没有办法做到这一点?
提前感谢您提供任何帮助&lt; 3
答案 0 :(得分:0)
在你的第二个双循环中
collisionStart[i - 1] = position;
应改为
collisionStart[i - 1] = new Vector2(...);
我不确定你从哪里获得职位。
但我认为你分配给Vector2数组的每个元素的是一些Vector2
整个数组指向同一个对象,因此你的输出在for循环之外。