java - For-loop gives only one value when iterating through an array of Vectors (libGDX) -
i'm beginner, here's small mistake can recognize yeah... i'm using libgdx library , here's code of method:
public vector2[] initcollisionstart() { int = 0; (int row = 0; row < currentmapheighttiles; row++) { (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]; (int row = 0; row < currentmapheighttiles; row++) { (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[" + + "] = " + 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[" + + "] = " + collisionstart[i - 1]); i--; } } } collisioninitialized = true; system.out.print("\n"); (int z = 0; z < collisionstart.length; z++) { system.out.println("collisionstart[" + z + "] = " + collisionstart[z]); } return collisionstart; }
and output looks that:
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]
and here's question. why every value of collisionstart array equals [280.0:5760.0] when use outside of double for-loop? expect return same value initialized earlier. there way this?
thanks in advance <3
in second double loop
collisionstart[i - 1] = position;
should changed to
collisionstart[i - 1] = new vector2(...);
i not sure getting position from.
assume vector2 assign every element of vector2 array.
entire array pointing same object, , hence output outside loop.
Comments
Post a Comment