Code edit (1 edits merged)
Please save this source code
User prompt
when i pass first level level 2 is do not interact with my input
User prompt
after changing level game is unplayable
User prompt
level 2 is unpassable
User prompt
level dont change after complete
User prompt
create 5 level
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'to')' in or related to this line: 'tw.to({' Line Number: 207 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'to')' in or related to this line: 'tween(cube).to({' Line Number: 206
User prompt
i want to move the cube which i tap with arrow direction
User prompt
objects dont move
User prompt
objects not move
User prompt
blue wont move
Code edit (1 edits merged)
Please save this source code
User prompt
Arrow Slide Grid
User prompt
i want to create game where cubes placed on grid and each cube has arrow to indicade which way can move if path is empty they can go if not they wont. if path empty cube slide all the way out of the screen. win state is clear all grids
User prompt
cubes move until out of screen and diseapper if all grid empty level complete
Initial prompt
i want to create game where cubes placed on grid and each cube has arrow to indicade which way can move if path is empty they can go if not they wont.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Cube class var Cube = Container.expand(function () { var self = Container.call(this); // Cube base var cubeGfx = self.attachAsset('cube', { anchorX: 0.5, anchorY: 0.5 }); // Arrow overlay self.arrowGfx = null; // Grid position self.gridX = 0; self.gridY = 0; // Direction index (0:up, 1:down, 2:left, 3:right) self.dir = 0; // Is this cube currently animating? self.isSliding = false; // Set direction and update arrow self.setDirection = function (dirIdx) { self.dir = dirIdx; if (self.arrowGfx) self.removeChild(self.arrowGfx); var dir = DIRS[dirIdx]; self.arrowGfx = self.attachAsset(dir.arrow, { anchorX: 0.5, anchorY: 0.5 }); // For left/right, rotate arrow to point correctly if (dir.name === 'left') { self.arrowGfx.rotation = Math.PI; } else if (dir.name === 'up') { self.arrowGfx.rotation = -Math.PI / 2; } else if (dir.name === 'down') { self.arrowGfx.rotation = Math.PI / 2; } else { self.arrowGfx.rotation = 0; } }; // Called when tapped self.down = function (x, y, obj) { if (self.isSliding) return; if (game.isAnimating) return; game.trySlideCube(self); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x222831 }); /**** * Game Code ****/ // Cube directions // Tween for sliding animation // Arrow assets: 120x60, colored arrows for each direction // Cube asset: 200x200, white box // Grid config var DIRS = [{ name: 'up', dx: 0, dy: -1, arrow: 'arrow_up', rot: 0 }, { name: 'down', dx: 0, dy: 1, arrow: 'arrow_down', rot: 0 }, { name: 'left', dx: -1, dy: 0, arrow: 'arrow_left', rot: 0 }, { name: 'right', dx: 1, dy: 0, arrow: 'arrow_right', rot: 0 }]; var GRID_SIZE = 4; var CELL_SIZE = 260; // 200px cube + 60px gap var GRID_ORIGIN_X = Math.floor((2048 - GRID_SIZE * CELL_SIZE) / 2) + CELL_SIZE / 2; var GRID_ORIGIN_Y = Math.floor((2732 - GRID_SIZE * CELL_SIZE) / 2) + CELL_SIZE / 2; // Game state var cubes = []; // 2D array [y][x] of Cube or null var cubeList = []; // Flat list of all Cube objects var isAnimating = false; // Score text var scoreTxt = new Text2('0', { size: 120, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Helper: get grid cell center position function gridToPos(gx, gy) { return { x: GRID_ORIGIN_X + gx * CELL_SIZE, y: GRID_ORIGIN_Y + gy * CELL_SIZE }; } // Helper: check if (gx,gy) is inside grid function inGrid(gx, gy) { return gx >= 0 && gx < GRID_SIZE && gy >= 0 && gy < GRID_SIZE; } // Helper: update score display function updateScore() { var left = 0; for (var y = 0; y < GRID_SIZE; ++y) { for (var x = 0; x < GRID_SIZE; ++x) { if (cubes[y][x]) left++; } } scoreTxt.setText(left + ""); } // Try to slide a cube in its direction game.trySlideCube = function (cube) { // Only allow one animation at a time if (game.isAnimating) return; if (cube.isSliding) return; var dir = DIRS[cube.dir]; var gx = cube.gridX; var gy = cube.gridY; var dx = dir.dx; var dy = dir.dy; // Find farthest empty cell in direction var nx = gx; var ny = gy; while (true) { var tx = nx + dx; var ty = ny + dy; if (!inGrid(tx, ty)) { // Out of grid, can slide out break; } if (cubes[ty][tx]) { // Blocked by another cube // Can't move return; } nx = tx; ny = ty; } // If not moving, do nothing if (nx === gx && ny === gy) return; // Mark animating cube.isSliding = true; game.isAnimating = true; // Remove from grid cubes[gy][gx] = null; // Animate to out-of-grid position var endPos; if (inGrid(nx, ny)) { // Should not happen, but fallback endPos = gridToPos(nx, ny); } else { // Compute position just outside grid var outX = nx; var outY = ny; // If moving right, outX is GRID_SIZE, left: -1 // If moving down, outY is GRID_SIZE, up: -1 endPos = { x: GRID_ORIGIN_X + nx * CELL_SIZE, y: GRID_ORIGIN_Y + ny * CELL_SIZE }; } // Animate slide tween(cube, { x: endPos.x, y: endPos.y }, { duration: 300, onFinish: function onFinish() { // Remove cube from scene and list cube.destroy(); var idx = cubeList.indexOf(cube); if (idx !== -1) cubeList.splice(idx, 1); cube.isSliding = false; game.isAnimating = false; updateScore(); // Check win var left = 0; for (var y = 0; y < GRID_SIZE; ++y) { for (var x = 0; x < GRID_SIZE; ++x) { if (cubes[y][x]) left++; } } if (left === 0) { LK.showYouWin(); } } }); }; function buildLevel() { // Clear previous for (var i = 0; i < cubeList.length; ++i) { cubeList[i].destroy(); } cubeList = []; cubes = []; for (var y = 0; y < GRID_SIZE; ++y) { var row = []; for (var x = 0; x < GRID_SIZE; ++x) row.push(null); cubes.push(row); } // Example level: 6 cubes, hand-crafted for solvability // (Can be randomized in future) var levelCubes = [{ x: 0, y: 0, dir: 3 }, // right { x: 1, y: 0, dir: 1 }, // down { x: 2, y: 1, dir: 2 }, // left { x: 3, y: 2, dir: 0 }, // up { x: 1, y: 3, dir: 3 }, // right { x: 2, y: 2, dir: 1 } // down ]; for (var i = 0; i < levelCubes.length; ++i) { var c = levelCubes[i]; var cube = new Cube(); cube.gridX = c.x; cube.gridY = c.y; cube.setDirection(c.dir); var pos = gridToPos(c.x, c.y); cube.x = pos.x; cube.y = pos.y; cubes[c.y][c.x] = cube; cubeList.push(cube); game.addChild(cube); } updateScore(); } // Prevent accidental drag game.move = function (x, y, obj) {}; // No drag game.down = function (x, y, obj) {}; game.up = function (x, y, obj) {}; // No per-tick logic needed game.update = function () {}; // Start game buildLevel();
===================================================================
--- original.js
+++ change.js
@@ -179,33 +179,33 @@
y: GRID_ORIGIN_Y + ny * CELL_SIZE
};
}
// Animate slide
- var tw = tween(cube);
- tw.to({
+ tween(cube, {
x: endPos.x,
y: endPos.y
- }, 300);
- tw.on('end', function () {
- // Remove cube from scene and list
- cube.destroy();
- var idx = cubeList.indexOf(cube);
- if (idx !== -1) cubeList.splice(idx, 1);
- cube.isSliding = false;
- game.isAnimating = false;
- updateScore();
- // Check win
- var left = 0;
- for (var y = 0; y < GRID_SIZE; ++y) {
- for (var x = 0; x < GRID_SIZE; ++x) {
- if (cubes[y][x]) left++;
+ }, {
+ duration: 300,
+ onFinish: function onFinish() {
+ // Remove cube from scene and list
+ cube.destroy();
+ var idx = cubeList.indexOf(cube);
+ if (idx !== -1) cubeList.splice(idx, 1);
+ cube.isSliding = false;
+ game.isAnimating = false;
+ updateScore();
+ // Check win
+ var left = 0;
+ for (var y = 0; y < GRID_SIZE; ++y) {
+ for (var x = 0; x < GRID_SIZE; ++x) {
+ if (cubes[y][x]) left++;
+ }
}
+ if (left === 0) {
+ LK.showYouWin();
+ }
}
- if (left === 0) {
- LK.showYouWin();
- }
});
- tw.start();
};
function buildLevel() {
// Clear previous
for (var i = 0; i < cubeList.length; ++i) {