IT WORKS!! not only that, also shorten my codes.
at first i stumbled into an error undefined tweens., so i tried to use different methods then i rewrite it back to original and it just works lol.
below are my code (note that my blocks are dynamically? generated) so i have to create new array to store the blocks and to loop to add listener, i called createField on create()
createField(fieldSize, blockSize, addEmpty, offsetPos, maxColor){
let row = offsetPos.y;
let column = offsetPos.x;
// console.log( this.game.scale.displayScale.x);
// console.log(fieldSize);
let blocksPos = [];
let centerPos = [];
let centerIndex = [{"x": 1, "y": 1}, {"x": 1, "y": 2},{"x": 1, "y": 3},{"x": 2, "y": 1},{"x": 2, "y": 2},{"x": 2, "y": 3},{"x": 3, "y": 1},{"x": 3, "y": 2},{"x": 3, "y": 3}];
// console.log(centerIndex);
for(let i = 0; i < fieldSize; i++){//row
row = ((i) * blockSize) + offsetPos.y;
// console.log(" . row " + (i+1) + " - " + row);
for(let j = 0; j < fieldSize; j++){//column
column = ((j) * blockSize) + offsetPos.x;
let blockPos = {"x": column, "y": row}
blocksPos.push(blockPos);
if(centerIndex.findIndex(pos => pos.x === i && pos.y === j) > -1){
centerPos.push(blockPos);
}
}
}
Phaser.Math.RND.shuffle(blocksPos);
Phaser.Math.RND.sow([139]);
let color = 1;
let j = 1;
let blocktba;
let blocks = [];
for(let i = 0; i < fieldSize*fieldSize; i++){//row
if(color != 7){
if(centerPos.findIndex(pos => pos.x === blocksPos[i].x && pos.y === blocksPos[i].y) > -1 && addEmpty){
blocktba = this.add.image(blocksPos[i].x, blocksPos[i].y, "blockAtlas", ('block'+(color)+ "_center"));
} else{
blocktba = this.add.image(blocksPos[i].x, blocksPos[i].y, "blockAtlas", ('block'+(color)));
}
if(j % maxColor == 0){
color++;
}
j++;
} else{
if(addEmpty){
blocktba = this.add.image(blocksPos[i].x, blocksPos[i].y, "blockAtlas", ('block_empty'));
}
}
let blockScale = blockSize / blocktba.width;
blocktba.setScale(blockScale).setInteractive();
blocks.push(blocktba);
}
if(blocks.length > 9){
for(let i = 0; i < blocks.length; i++){
blocks[i].on("pointerdown", ()=>{
let itemPos = {"x": blocks[i].x, "y": blocks[i].y};
let mainBlock = blocks.find(o=> o.frame.name == "block_empty");
let mainPos = {"x": mainBlock.x, "y": mainBlock.y};
// let diffPos = {"x": (itemPos.x - mainBlock.x), "y": (itemPos.y - mainBlock.y)};
// console.log(diffPos);
if ((Math.abs(itemPos.x - mainBlock.x) == 128 && Math.abs(itemPos.y - mainBlock.y) == 0) || (Math.abs(itemPos.x - mainBlock.x) == 0 && Math.abs(itemPos.y - mainBlock.y) == 128)){
// this.moveBlock(mainBlock, blocks[i], mainPos, itemPos);
this.tweens.add({
targets: mainBlock,
x: itemPos.x,
y: itemPos.y,
ease: 'Power1',
duration: 150,
onComplete: () => {
mainBlock.x = itemPos.x;
mainBlock.y = itemPos.y;
}
});
this.tweens.add({
targets: blocks[i],
x: mainPos.x,
y: mainPos.y,
ease: 'Power1',
duration: 150,
onComplete: () => {
blocks[i].x = mainPos.x;
blocks[i].y = mainPos.y;
}
});
}
})
}
}
// console.log(blocksPos);
// let block = this.add.image(column, row, "blockAtlas", ("block"+(color)));
}