User prompt
Tiles should only be able to be merged once per move
Code edit (1 edits merged)
Please save this source code
User prompt
return true or false from mergeTiles based of if a tile was merged or not
User prompt
In the merge section of moveTiles only set moved to true if mergeTiles returns true
User prompt
Only insert random tile on board, if the board was changed as a result of the move action
User prompt
When inserting new tiles, initialize them at their target x,y position
User prompt
New tiles should spawn at their final position
User prompt
When spawning a new tile make sure to set targetX and targetY as well
User prompt
When spawning a new tile, make it spawn with x,y of it's final destination
User prompt
Add an instant property to setPosition that if true, set's the tile x,y to the final destination instantly
User prompt
When calling setPosition on newly created tiles, make sure instant is set to true
User prompt
moveTiles make sure the iteration happens in the direction of movement. This is to ensure tiles merge correctly similar to 2048
User prompt
Reverse the loop directions in moveTiles
User prompt
Sometimes the tiles do not move to the swipe direction when merging. Can you fix this?
User prompt
Fix Bug: 'TypeError: null is not an object (evaluating 'squares[x][y].setPosition')' in this line: 'squares[x][y].setPosition(x, y);' Line Number: 137
User prompt
Fix Bug: 'TypeError: null is not an object (evaluating 'squares[x][y].setPosition')' in this line: 'squares[x][y].setPosition(x, y);' Line Number: 139
User prompt
Reverse the up / down merge direction
User prompt
Reverse the left right merge direction
User prompt
Reverse which tile is kept in mergeTiles
User prompt
Reverse the up down merge direvtion
User prompt
Reverse the left right merge direction
User prompt
Don't destroy merged tiles right away, instead move them to a toBeRemoved array
User prompt
On tiles that where removed, set their target target x and y to the x and y of the tile they merged into
User prompt
in tick, move toRemoveTiles, when move returns false remove from the array and destroy the asset
User prompt
in tick, call move on tiles to be removed, then if move returns false, destroy the asset
var Tile = Container.expand(function (value) {
var self = Container.call(this);
self.value = value || 2;
var tileGraphics = self.createAsset('tile', 'Tile Graphics', 0, 0);
var tileLabel = new Text2(self.value.toString(), {
size: 300,
fill: '#ffffff',
font: 'bold'
});
tileLabel.anchor.set(0.5, 0.5);
tileLabel.x = tileGraphics.width / 2;
tileLabel.y = tileGraphics.height / 2;
tileGraphics.addChild(tileLabel);
self.setValue = function (value) {
self.value = value;
tileLabel.setText(self.value.toString());
};
self.targetX = 0;
self.targetY = 0;
self.isMoving = false;
self.merged = false;
self.setPosition = function (x, y) {
self.targetX = x * 500;
self.targetY = y * 500;
};
self.move = function () {
var threshold = 5;
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
if (Math.abs(dx) > threshold || Math.abs(dy) > threshold) {
self.isMoving = true;
self.x += dx * 0.2;
self.y += dy * 0.2;
return false;
}
self.isMoving = false;
self.x = self.targetX;
self.y = self.targetY;
return true;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
self.addRandomTile = function () {
var emptyPositions = [];
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] === null) {
emptyPositions.push({
x: i,
y: j
});
}
}
}
if (emptyPositions.length > 0) {
var randomPosition = emptyPositions[Math.floor(Math.random() * emptyPositions.length)];
var square = new Tile();
square.setPosition(randomPosition.x, randomPosition.y);
squares[randomPosition.x][randomPosition.y] = square;
gridContainer.addChild(square);
}
};
self.mergeTiles = function (x1, y1, x2, y2) {
if (squares[x1][y1] !== null && squares[x2][y2] !== null && squares[x1][y1].value === squares[x2][y2].value && !squares[x1][y1].merged && !squares[x2][y2].merged) {
squares[x1][y1].setValue(squares[x1][y1].value * 2);
squares[x1][y1].merged = true;
squares[x2][y2].destroy();
squares[x2][y2] = null;
}
};
var gridSize = 4;
LK.stageContainer.setBackgroundColor(0xD3D3D3);
var gridContainer = new Container();
gridContainer.x = 44;
gridContainer.y = 200;
self.addChild(gridContainer);
var squares = new Array(gridSize).fill().map(() => new Array(gridSize).fill(null));
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
squares[i][j] = null;
}
}
var initialTiles = 2;
for (var i = 0; i < initialTiles; i++) {
var x, y;
do {
x = Math.floor(Math.random() * gridSize);
y = Math.floor(Math.random() * gridSize);
} while (squares[x][y] !== null);
var square = new Tile();
square.setPosition(x, y);
squares[x][y] = square;
gridContainer.addChild(square);
}
var swipeStart = {
x: 0,
y: 0
};
var swipeEnd = {
x: 0,
y: 0
};
self.moveTiles = function (direction) {
var dx = (direction === 'right') - (direction === 'left');
var dy = (direction === 'down') - (direction === 'up');
var moved = false;
do {
moved = false;
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
var x = i + dx;
var y = j + dy;
if (x >= 0 && x < gridSize && y >= 0 && y < gridSize && squares[i][j] !== null) {
if (squares[x][y] === null) {
squares[x][y] = squares[i][j];
squares[i][j] = null;
squares[x][y].setPosition(x, y);
moved = true;
} else if (squares[x][y] !== null && squares[i][j] !== null && squares[x][y].value === squares[i][j].value) {
self.mergeTiles(i, j, x, y);
moved = true;
}
}
}
}
} while (moved);
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] !== null) {
squares[i][j].merged = false;
}
}
}
};
stage.on('down', function (obj) {
var anyTileMoving = squares.some(row => row.some(tile => tile && tile.isMoving));
if (!anyTileMoving) {
var pos = obj.event.getLocalPosition(self);
swipeStart = {
x: pos.x,
y: pos.y
};
}
});
stage.on('up', function (obj) {
var pos = obj.event.getLocalPosition(self);
swipeEnd = {
x: pos.x,
y: pos.y
};
var dx = swipeEnd.x - swipeStart.x;
var dy = swipeEnd.y - swipeStart.y;
var threshold = 50;
if (Math.abs(dx) > threshold || Math.abs(dy) > threshold) {
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
self.moveTiles('right');
} else {
self.moveTiles('left');
}
} else {
if (dy > 0) {
self.moveTiles('down');
} else {
self.moveTiles('up');
}
}
}
var anyTileMoving = squares.some(row => row.some(tile => tile && tile.isMoving));
if (!anyTileMoving) {
self.addRandomTile();
}
});
LK.on('tick', function () {
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
if (squares[i][j] !== null) {
squares[i][j].move();
}
}
}
});
});