User prompt
Fix Bug: 'TypeError: Cannot set properties of undefined (setting 'alpha')' in this line: 'cell.alpha = 1;' Line Number: 204
User prompt
Fix Bug: 'TypeError: Cannot set properties of undefined (setting 'alpha')' in this line: 'cell.alpha = 1;' Line Number: 204
Code edit (2 edits merged)
Please save this source code
User prompt
if selected cells === 10 then reset selected cells
Code edit (2 edits merged)
Please save this source code
User prompt
add if condition on cell.on. if selectedcells.length = 1 then self.resetselctedcells()
User prompt
update cell ono up to empt selected cells
User prompt
Fix Bug: 'ReferenceError: selectedCells is not defined' in this line: 'selectedCellsCountTxt.setText(selectedCells.length.toString());' Line Number: 273
User prompt
Fix Bug: 'ReferenceError: selectedCells is not defined' in this line: 'selectedCellsCountTxt.setText(selectedCells.length.toString());' Line Number: 273
User prompt
add text in screen that will show selectedcells.count and display it on tick
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in this line: 'selectedCellsCountText.setText(self.selectedCells.length.toString());' Line Number: 287
User prompt
Fix Bug: 'ReferenceError: selectedCells is not defined' in this line: 'selectedCellsCountText.setText(selectedCells.length.toString());' Line Number: 286
User prompt
show selecetedcells length text on the center of the screen on tick
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in this line: 'selectedCellsText.setText('Selected Cells: ' + self.selectedCells.length);' Line Number: 274
User prompt
Fix Bug: 'ReferenceError: selectedCells is not defined' in this line: 'selectedCellsText.setText('Selected Cells: ' + selectedCells.length);' Line Number: 274
User prompt
update selected cells on tick
User prompt
Fix Bug: 'ReferenceError: selectedCells is not defined' in this line: 'selectedCellsText.setText('Selected Cells: ' + selectedCells.length);' Line Number: 273
User prompt
Fix Bug: 'ReferenceError: selectedCells is not defined' in this line: 'selectedCellsText.setText('Selected Cells: ' + selectedCells.length);' Line Number: 273
User prompt
show selecetedcells text in center of screen on tick
User prompt
Resetselectedcells on tick
User prompt
Add global condition. If there is no touch on screen, resetselectedcells
User prompt
Run resetselectedcells on tick whenever ther is no touch on schreen
User prompt
End of touch can happen anywhere not only on top of the cell
User prompt
Force empty of selected cells after every touch release
var Particle = Container.expand(function (tint) { var self = Container.call(this); self.tint = tint; var particleGraphics = self.createAsset('particle', 'Particle Graphics', 0.5, 0.5); particleGraphics.rotation = Math.random() * Math.PI * 2; particleGraphics.tint = self.tint; self.vx = Math.random() * 4 - 2; self.vy = Math.random() * 4 - 2; self.alpha = 1; self.lifetime = 60; self.tick = function () { self.x += self.vx; self.y += self.vy; self.alpha -= 1 / self.lifetime; if (self.alpha <= 0) self.destroy(); }; }); var Cell = Container.expand(function (type) { var self = Container.call(this); var cellGraphics = self.createAsset('cell', 'Grid Cell', .5, .5); self.targetX = 0; self.targetY = 0; self.isMoving = false; self.totalTypes = 5; self.type = type || Math.floor(Math.random() * self.totalTypes); var hue = self.type / self.totalTypes; var color = hsvToRgb(hue, 1, 1); cellGraphics.tint = color; self.move = function (x, y, instant) { self.targetX = x; self.targetY = y; if (instant) { self.x = x; self.y = y; } }; self.tick = function () { var acceleration = 1; self.speedX = self.speedX || 0; self.speedY = self.speedY || 0; var threshold = 1; var dx = self.targetX - self.x; var dy = self.targetY - self.y; if (Math.abs(dx) < threshold && Math.abs(dy) < threshold) { self.x = self.targetX; self.y = self.targetY; self.isMoving = false; self.speedX = 0; self.speedY = 0; } else { if (dx !== 0) { self.speedX += dx > 0 ? acceleration : -acceleration; } if (dy !== 0) { self.speedY += dy > 0 ? acceleration : -acceleration; } var nextX = self.x + self.speedX; var nextY = self.y + self.speedY; if (self.speedX > 0 && nextX > self.targetX || self.speedX < 0 && nextX < self.targetX) { nextX = self.targetX; self.speedX = 0; } if (self.speedY > 0 && nextY > self.targetY || self.speedY < 0 && nextY < self.targetY) { nextY = self.targetY; self.speedY = 0; } self.x = nextX; self.y = nextY; self.isMoving = self.x !== self.targetX || self.y !== self.targetY; } }; }); function hsvToRgb(h, s, v) { var i = Math.floor(h * 6), f = h * 6 - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s), mod = i % 6, r = [v, q, p, p, t, v][mod], g = [t, v, v, q, p, p][mod], b = [p, p, t, v, v, q][mod]; return (r * 255 << 16) + (g * 255 << 8) + b * 255; } var Game = Container.expand(function () { var self = Container.call(this); var selectedCellsCountTxt = new Text2('0', { size: 100, fill: '#ffffff', align: 'center' }); selectedCellsCountTxt.anchor.set(0.5, 0); selectedCellsCountTxt.x = 2048 / 2; selectedCellsCountTxt.y = 100; LK.gui.topCenter.addChild(selectedCellsCountTxt); self.particles = []; self.spawnParticles = function (x, y, color) { for (var i = 0; i < 10; i++) { var particle = new Particle(color); particle.x = gridContainer.x + x; particle.y = gridContainer.y + y; particle.tint = color; self.particles.push(particle); self.addChild(particle); } }; self.checkMovesAllowed = function () { for (var i = 0; i < gridWidth; i++) { for (var j = 0; j < gridHeight; j++) { var cell = grid[i][j]; if (cell) { var neighbors = self.findConnectedNeighbors(cell); if (neighbors.length > 1) { return true; } } } } return false; }; var gridWidth = 5; var gridHeight = 5; var gridSpacing = 8; var tempCell = new Cell(); var cellWidth = tempCell.width; var cellHeight = tempCell.height; tempCell.destroy(); var gridContainer = new Container(); self.addChild(gridContainer); var totalGridWidth = gridWidth * (cellWidth + gridSpacing) - gridSpacing; var totalGridHeight = gridHeight * (cellHeight + gridSpacing) - gridSpacing; gridContainer.x = (2048 - totalGridWidth) / 2 + cellWidth / 2; gridContainer.y = (2732 - totalGridHeight) / 2 + cellHeight / 2; self.calculateTargetPosition = function (col, row) { return { x: col * (cellWidth + gridSpacing), y: row * (cellHeight + gridSpacing) }; }; self.findConnectedNeighbors = function (cell, connectedNeighbors) { connectedNeighbors = connectedNeighbors || []; if (!cell) return []; var cellType = cell.type; var cellColRow = self.findCellColRow(cell); if (cellColRow) { var directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; directions.forEach(function (dir) { var newRow = cellColRow.row + dir[0]; var newCol = cellColRow.col + dir[1]; if (newRow >= 0 && newRow < gridHeight && newCol >= 0 && newCol < gridWidth) { var neighborCell = grid[newCol][newRow]; if (neighborCell && neighborCell.type === cellType && connectedNeighbors.indexOf(neighborCell) === -1) { connectedNeighbors.push(neighborCell); self.findConnectedNeighbors(neighborCell, connectedNeighbors); } } }); } return connectedNeighbors; }; self.findCellColRow = function (cell) { for (var col = 0; col < gridWidth; col++) { for (var row = 0; row < gridHeight; row++) { if (grid[col][row] === cell) { return { col: col, row: row }; } } } return null; }; self.deleteCell = function (cell) { var colRow = self.findCellColRow(cell); if (colRow) { grid[colRow.col][colRow.row] = null; var hue = cell.type / cell.totalTypes; var color = hsvToRgb(hue, 1, 1); self.spawnParticles(cell.x, cell.y, color); cell.destroy(); self.moveToEmptySpace(); } }; self.attachCellListeners = function (cell) { var dragStartCell = null; var selectedCells = []; self.resetSelectedCells = function () { selectedCells.forEach(function (cell) { cell.alpha = 1; }); selectedCells = []; }; cell.on('down', function (obj) { dragStartCell = cell; selectedCells.push(cell); cell.alpha = 0.5; }); cell.on('move', function (obj) { if (dragStartCell) { var pos = obj.event.getLocalPosition(gridContainer); var col = Math.floor((pos.x + cellWidth / 2) / (cellWidth + gridSpacing)); var row = Math.floor((pos.y + cellHeight / 2) / (cellHeight + gridSpacing)); if (col >= 0 && col < gridWidth && row >= 0 && row < gridHeight) { var targetCell = grid[col][row]; if (targetCell && selectedCells.indexOf(targetCell) === -1) { if (targetCell.type === dragStartCell.type) { targetCell.alpha = 0.5; selectedCells.push(targetCell); } else { self.resetSelectedCells(); dragStartCell = null; } } } } }); cell.on('up', function (obj) { if (selectedCells.length >= 4) { for (var col = 0; col < gridWidth; col++) { for (var row = 0; row < gridHeight; row++) { var cell = grid[col][row]; if (cell && dragStartCell && cell.type === dragStartCell.type) { self.deleteCell(cell); } } } } else if (selectedCells.length > 1) { for (var i = 0; i < selectedCells.length; i++) { self.deleteCell(selectedCells[i]); } } self.resetSelectedCells(); dragStartCell = null; }); }; var grid = []; for (var i = 0; i < gridWidth; i++) { grid[i] = []; for (var j = 0; j < gridHeight; j++) { var cell = new Cell(); var targetPos = self.calculateTargetPosition(i, j); cell.move(targetPos.x, targetPos.y, true); self.attachCellListeners(cell); grid[i][j] = cell; gridContainer.addChild(cell); } } self.moveToEmptySpace = function () { var moved; do { moved = false; for (var col = 0; col < gridWidth; col++) { for (var row = gridHeight - 2; row >= 0; row--) { if (grid[col][row] && !grid[col][row + 1]) { var targetCell = grid[col][row]; var targetPos = self.calculateTargetPosition(col, row + 1); targetCell.move(targetPos.x, targetPos.y); grid[col][row + 1] = targetCell; grid[col][row] = null; moved = true; } } } } while (moved); for (var col = 0; col < gridWidth; col++) { if (!grid[col][0]) { var newCell = new Cell(); var targetPos = self.calculateTargetPosition(col, 0); newCell.x = targetPos.x; newCell.y = targetPos.y - cellHeight - 200; newCell.move(targetPos.x, targetPos.y); self.attachCellListeners(newCell); grid[col][0] = newCell; gridContainer.addChildAt(newCell, 0); self.moveToEmptySpace(); } } }; LK.on('tick', function () { selectedCellsCountTxt.setText(selectedCells.length.toString()); for (var i = 0; i < gridWidth; i++) { for (var j = 0; j < gridHeight; j++) { if (grid[i][j]) grid[i][j].tick(); } } for (var i = self.particles.length - 1; i >= 0; i--) { var particle = self.particles[i]; particle.tick(); if (particle.lifeSpan <= 0) { particle.destroy(); self.particles.splice(i, 1); } } if (!self.checkMovesAllowed()) { if (!self.gameOverTimeout) { self.gameOverTimeout = LK.setTimeout(function () { LK.showGameOver(); }, 2000); } } else { if (self.gameOverTimeout) { LK.clearTimeout(self.gameOverTimeout); self.gameOverTimeout = null; } } }); });
===================================================================
--- original.js
+++ change.js
@@ -75,17 +75,17 @@
return (r * 255 << 16) + (g * 255 << 8) + b * 255;
}
var Game = Container.expand(function () {
var self = Container.call(this);
- var selectedCellsCountText = new Text2('0', {
- size: 150,
- fill: "#ffffff",
+ var selectedCellsCountTxt = new Text2('0', {
+ size: 100,
+ fill: '#ffffff',
align: 'center'
});
- selectedCellsCountText.anchor.set(0.5, 0.5);
- selectedCellsCountText.x = 2048 / 2;
- selectedCellsCountText.y = 2732 / 2;
- LK.gui.addChild(selectedCellsCountText);
+ selectedCellsCountTxt.anchor.set(0.5, 0);
+ selectedCellsCountTxt.x = 2048 / 2;
+ selectedCellsCountTxt.y = 100;
+ LK.gui.topCenter.addChild(selectedCellsCountTxt);
self.particles = [];
self.spawnParticles = function (x, y, color) {
for (var i = 0; i < 10; i++) {
var particle = new Particle(color);
@@ -226,9 +226,8 @@
dragStartCell = null;
});
};
var grid = [];
- var selectedCells = [];
for (var i = 0; i < gridWidth; i++) {
grid[i] = [];
for (var j = 0; j < gridHeight; j++) {
var cell = new Cell();
@@ -270,8 +269,9 @@
}
}
};
LK.on('tick', function () {
+ selectedCellsCountTxt.setText(selectedCells.length.toString());
for (var i = 0; i < gridWidth; i++) {
for (var j = 0; j < gridHeight; j++) {
if (grid[i][j]) grid[i][j].tick();
}
@@ -283,9 +283,8 @@
particle.destroy();
self.particles.splice(i, 1);
}
}
- selectedCellsCountText.setText(selectedCells.length.toString());
if (!self.checkMovesAllowed()) {
if (!self.gameOverTimeout) {
self.gameOverTimeout = LK.setTimeout(function () {
LK.showGameOver();