- Creator 版本: 2.4.5
用for循环创建6个节点,分别在6个位置,通过算法赋值给变量,但是位置不是我想要的位置,我以为是算法算的不对,然后log出来看一下,赋值之后的数竟然会变,这是什么逻辑,大佬们帮看看
this.picArr = [];
for (let i = 0; i < 2; i++) {
this.picArr[i] = [];
for (let j = 0; j < 3; j++) {
this.picArr[i][j] = cc.instantiate(this.block);
this.picArr[i][j].parent = background;
let margin = 50;
let parentWidht = this.node.width;
let parentHeight = this.node.height;
let targetPosX = Math.abs(i * (parentWidht - this.picArr[i][j].width) - margin);
let targetPosY = (- parentHeight + this.picArr[i][j].height) / 2 * j + (j - 1) * margin;
this.picArr[i][j].getComponent(‘block’).init(texture, cc.v2(j, i));
this.picArr[i][j].setPosition(cc.v2(targetPosX, targetPosY));
cc.log(targetPosX, targetPosY);
cc.log(Math.abs(i * (parentWidht - this.picArr[i][j].width) - margin), (- parentHeight + this.picArr[i][j].height) / 2 * j + (j - 1) * margin);
}
}
log出来的结果竟然是这样的,除了第一个节点的位置是正确的,其他的位置都不对,我就看不懂是哪里出问题了
50 -50
50 -50
50 -320
50 -162.5
50 -590
50 -275
910 -50
548 -50
910 -320
548 -162.5
910 -590
548 -275