Hey everyone,
I have two 2D arrays in my project, one containing the initial order of image elements and the other containing their respective initial positions. While swapping the entities, only the first array should change, because the image elements must go to preset positions. The arrays are as follows.
this.grid = [
["Image1", "Image2", "Image3", "Image4"],
["Image5", "Image6", "Image7", "Image8"],
["Image9", "Image10", "Image11", "Image12"],
["Image13", "Image14", "Image15", "Image16"]
];
this.posGrid = [
[new pc.Vec3(-399.573, 271.648, -87.801), new pc.Vec3(-227.361, 271.648, -87.801), new pc.Vec3(-56.617, 271.648, -87.801), new pc.Vec3(109.018, 271.648, -87.801)],
[new pc.Vec3(-399.573, 105.57, -87.801), new pc.Vec3(-227.361, 105.57, -87.801), new pc.Vec3(-56.617, 105.57, -87.801), new pc.Vec3(109.018, 105.57, -87.801)],
[new pc.Vec3(-399.573, -67.952, -87.801), new pc.Vec3(-227.361, -67.952, -87.801), new pc.Vec3(-56.617, -67.952, -87.801), new pc.Vec3(109.018, -67.952, -87.801)],
[new pc.Vec3(-399.573, -235.868, -87.801), new pc.Vec3(-227.361, -235.868, -87.801), new pc.Vec3(-56.617, -235.868, -87.801), new pc.Vec3(109.018, -235.868, -87.801)]
];
I have an array containing currently the first and third images and their boundaries. It only contains them for Image1 and Image3 as I was testing with just these two.
let arr2 = [this.grid[0][0], this.s1xmax, this.s1xmin, this.s1ymax, this.s1ymin, this.grid[0][2], this.s3xmax, this.s3xmin, this.s3ymax, this.s3ymin];
I then check upon the mouseup
event whether the entity being clicked is within the boundaries of it’s nearby images. If it is, then I swap them in the grid, and then attempt to swap their positions.
this.entity.element.on('mouseup', function (event) {
let len = (this.getNearOnes(this.entity.name).length);
rt = 0;
while (rt < len) {
if (this.getNearOnes(this.entity.name)[rt] !== null && this.getNearOnes(this.entity.name)[rt] !== undefined) {
for (let i=0; i<arr2.length; i++) {
if (arr2[i] === this.getNearOnes(this.entity.name)[rt]) {
//alert(i);
let xmax = arr2[i+1];
let xmin = arr2[i+2];
let ymax = arr2[i+3];
let ymin = arr2[i+4];
let myPos = this.entity.getLocalPosition();
if ((myPos.x <= xmax && myPos.x >= xmin) && (myPos.y <= ymax && myPos.y >= ymin)) {
//alert("check");
this.yourIndex = this.getIndex(arr2[i]);
this.myIndex = this.getIndex(this.entity.name);
this.x = this.yourIndex[0];
this.y = this.yourIndex[1];
this.grid[this.myIndex[0]][this.myIndex[1]] = this.grid[this.yourIndex[0]][this.yourIndex[1]];
this.grid[this.x][this.y] = this.entity.name;
alert(this.posGrid[this.x][this.y]);
this.app.root.findByName(arr2[i]).setLocalPosition(this.posGrid[this.myIndex[0]][this.myIndex[1]]);
this.entity.setLocalPosition(this.posGrid[this.x][this.y]);
}
}
}
}
rt++;
}
}, this);
My error is this. Although Image2 and Image1 switch perfectly, when I switch Image2 and Image3, Image2 returns back to its original position. I can guess why this happens - since their array indexes change, the position also remains the same. However, I am baffled as to why the same doesn’t happen for Image1. I haven’t hardcoded much, so it’s very odd.
Could someone please help me out here?