/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Tile class to represent each tile in the game var Tile = Container.expand(function () { var self = Container.call(this); self.value = 0; // Initial value of the tile self.graphics = self.attachAsset('tile', { anchorX: 0.5, anchorY: 0.5 }); self.text = new Text2(self.value.toString(), { fontSize: 50, fill: "#ffffff", align: "center" }); self.text.anchor.set(0.5, 0.5); self.addChild(self.text); self.updateValue = function (newValue) { self.value = newValue; self.text.setText(self.value.toString()); // Removed incorrect setText call on graphics object }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); // Initialize game variables var gridSize = 4; var tileSize = 200; var grid = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Initialize the grid with empty tiles for (var i = 0; i < gridSize; i++) { grid[i] = []; for (var j = 0; j < gridSize; j++) { var tile = new Tile(); tile.x = j * tileSize + tileSize / 2; tile.y = i * tileSize + tileSize / 2; grid[i][j] = tile; game.addChild(tile); } } // Function to add a new tile to the grid function addNewTile() { var emptyTiles = []; for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { if (grid[i][j].value === 0) { emptyTiles.push(grid[i][j]); } } } if (emptyTiles.length > 0) { var randomTile = emptyTiles[Math.floor(Math.random() * emptyTiles.length)]; randomTile.updateValue(2); } } // Function to handle swipe events function handleSwipe(direction) { var moved = false; if (direction === 'left') { for (var i = 0; i < gridSize; i++) { for (var j = 1; j < gridSize; j++) { if (grid[i][j].value !== 0) { var k = j; while (k > 0 && grid[i][k - 1].value === 0) { grid[i][k - 1].updateValue(grid[i][k].value); grid[i][k].updateValue(0); k--; moved = true; } if (k > 0 && grid[i][k - 1].value === grid[i][k].value) { grid[i][k - 1].updateValue(grid[i][k - 1].value * 2); grid[i][k].updateValue(0); score += grid[i][k - 1].value; scoreTxt.setText(score.toString()); moved = true; } } } } } // Handle other directions similarly... if (moved) { addNewTile(); } } // Event listeners for swipe events game.down = function (x, y, obj) { this.startX = x; this.startY = y; }; game.up = function (x, y, obj) { var deltaX = x - this.startX; var deltaY = y - this.startY; if (Math.abs(deltaX) > Math.abs(deltaY)) { if (deltaX > 0) { handleSwipe('right'); } else { handleSwipe('left'); } } else { if (deltaY > 0) { handleSwipe('down'); } else { handleSwipe('up'); } } }; // Initialize the game with two tiles addNewTile(); addNewTile();
===================================================================
--- original.js
+++ change.js
@@ -27,14 +27,21 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
+var background = LK.getAsset('background', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 2732 / 2
+});
+game.addChild(background);
// Initialize game variables
var gridSize = 4;
var tileSize = 200;
var grid = [];