User prompt
change the maze shape for level1
User prompt
Do another shape for maze level1 with few spaces beside each coin there is a way to go to it and collect it
User prompt
change the shape and reduce spaces
User prompt
do many walls of small walls of maze for level1 and check if any coin inside a wall change its position before loading.
User prompt
do many walls of small walls of maze for level1 and check if any coin inside a wall change its position before loading.
User prompt
make it difficult the maze of level1 by adding more walls and spaces between edges for player to pass through it :)
User prompt
change maze level1 let many spaces
User prompt
change the shape of maze of level1 and change coins position to not been inside the walls.
User prompt
change the shape of level1 maze walls
User prompt
Add level text beside pause close to it level1...level10
User prompt
Change the shape of maze of level1 to be more hard and let spaces between the horizontal and vertical edges to be double the size of player so he can pass .
User prompt
let the space road for the player be double of its size.
User prompt
For level1 do more walls and more spaces in each area have coin there must be a space on wall to let player pass to collect it.
User prompt
Add more walls for level1 and let a road spaces for the player to pass through it
User prompt
make the maze hard
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var localPos = game.toLocal(obj.parent.toGlobal(obj.position)); // Get position relative to game stage' Line Number: 438
Code edit (1 edits merged)
Please save this source code
User prompt
Maze Coin Challenge
Initial prompt
Create maze have 10 coins doing rotation 360° continuously when game start, do 10 levels each level have different shape of the maze you can do the 5 other levels like circle mazes have curved lines or walls, the maze have to be inside 4 boundaries of the screen, you can do 4 walls around the maze. do timer on the top left 1:00min if reach 0 before collecting all coins do gameover but if finished them all 10/10 tap screen to go to next level.
/****
* Classes
****/
// Coin class
var Coin = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.rotationSpeed = 0.05; // Radians per frame
self.update = function () {
self.rotation += self.rotationSpeed;
};
return self;
});
// Base size, will be scaled in code
// No plugins needed for this version. Tween could be used for rotation, but manual rotation is sufficient.
// var tween = LK.import('@upit/tween.v1');
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
// No update needed here as movement is handled in the main game loop
return self;
});
// Wall class
var Wall = Container.expand(function (width, height) {
var self = Container.call(this);
// We get a base wall asset and scale it to the desired dimensions
var graphics = self.attachAsset('wall', {
anchorX: 0.0,
// Anchor top-left for easier positioning and scaling
anchorY: 0.0,
width: width,
// Set the actual width
height: height // Set the actual height
});
// Store dimensions for collision checks
self.wallWidth = width;
self.wallHeight = height;
// No update needed for static walls
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x111111 // Dark background
});
/****
* Game Code
****/
// Define assets used in the game. These will be automatically created and loaded.
// Game constants and variables
var gameWidth = 2048;
var gameHeight = 2732;
var levelTimeLimit = 60; // Seconds
var totalLevels = 10; // As per description
var requiredCoinsPerLevel = 10;
// Maze definitions (simplified for MVP - add more levels later)
// Each level: walls array [{x, y, width, height}], coins array [{x, y}], startPos {x, y}
var mazeLevels = [
// Level 1 (Simple Box)
{
walls: [{
x: 100,
y: 200,
width: 1848,
height: 50
},
// Top border
{
x: 100,
y: 2482,
width: 1848,
height: 50
},
// Bottom border
{
x: 100,
y: 250,
width: 50,
height: 2232
},
// Left border
{
x: 1898,
y: 250,
width: 50,
height: 2232
},
// Right border
{
x: 500,
y: 800,
width: 1000,
height: 50
} // Internal horizontal
],
coins: [{
x: 300,
y: 400
}, {
x: 600,
y: 600
}, {
x: 900,
y: 400
}, {
x: 1200,
y: 600
}, {
x: 1500,
y: 400
}, {
x: 300,
y: 1200
}, {
x: 600,
y: 1000
}, {
x: 900,
y: 1200
}, {
x: 1200,
y: 1000
}, {
x: 1500,
y: 1200
}],
startPos: {
x: 200,
y: 350
}
},
// Level 2 (More walls) - TODO: Add 8 more levels
{
walls: [{
x: 100,
y: 200,
width: 1848,
height: 50
},
// Top
{
x: 100,
y: 2482,
width: 1848,
height: 50
},
// Bottom
{
x: 100,
y: 250,
width: 50,
height: 2232
},
// Left
{
x: 1898,
y: 250,
width: 50,
height: 2232
},
// Right
{
x: 100,
y: 700,
width: 800,
height: 50
},
// H-Wall 1
{
x: 1148,
y: 700,
width: 800,
height: 50
},
// H-Wall 2
{
x: 500,
y: 1200,
width: 1048,
height: 50
},
// H-Wall 3
{
x: 999,
y: 250,
width: 50,
height: 1500
},
// V-Wall 1
{
x: 500,
y: 1600,
width: 50,
height: 882
} // V-Wall 2
],
coins: [{
x: 250,
y: 350
}, {
x: 1700,
y: 350
}, {
x: 500,
y: 500
}, {
x: 1500,
y: 500
}, {
x: 250,
y: 1000
}, {
x: 1700,
y: 1000
}, {
x: 750,
y: 1500
}, {
x: 1250,
y: 1500
}, {
x: 300,
y: 2000
}, {
x: 1700,
y: 2000
}],
startPos: {
x: 200,
y: 350
}
}
// NOTE: Circular mazes are hard to represent with rectangular walls.
// This implementation uses only rectangular walls.
];
// Game state variables
var currentLevelIndex = 0;
var walls = [];
var coins = [];
var player = null;
var score = 0;
var timeLeft = levelTimeLimit;
var timerInterval = null;
var scoreTxt = null;
var timerTxt = null;
var levelComplete = false;
var isDragging = false;
var dragOffset = {
x: 0,
y: 0
}; // Offset between touch point and player center
// --- Helper Functions ---
// Clears current level elements
function clearLevel() {
walls.forEach(function (wall) {
wall.destroy();
});
walls = [];
coins.forEach(function (coin) {
coin.destroy();
});
coins = [];
if (player) {
player.destroy();
player = null;
}
levelComplete = false;
isDragging = false;
}
// Loads a level by index
function loadLevel(levelIndex) {
clearLevel();
if (levelIndex >= mazeLevels.length) {
// Handle case where we run out of predefined levels but haven't reached totalLevels
console.log("Warning: Ran out of predefined maze data, repeating last level.");
levelIndex = mazeLevels.length - 1; // Repeat the last available level
// Ideally, generate levels procedurally or add more data.
if (levelIndex < 0) {
// Should not happen if mazeLevels has data
LK.showGameOver(); // No levels defined at all
return;
}
}
currentLevelIndex = levelIndex; // Update the global index tracker
var levelData = mazeLevels[levelIndex];
// Create walls
levelData.walls.forEach(function (wallData) {
var wall = new Wall(wallData.width, wallData.height);
wall.x = wallData.x;
wall.y = wallData.y;
game.addChild(wall);
walls.push(wall);
});
// Create coins
levelData.coins.forEach(function (coinData) {
var coin = new Coin();
coin.x = coinData.x;
coin.y = coinData.y;
game.addChild(coin);
coins.push(coin);
});
// Create player
player = new Player();
player.x = levelData.startPos.x;
player.y = levelData.startPos.y;
game.addChild(player);
// Reset score and timer for the new level
score = 0;
timeLeft = levelTimeLimit;
updateScoreDisplay();
updateTimerDisplay();
startTimer();
}
// Checks collision between player (at potential new position) and all walls
function checkWallCollision(targetX, targetY) {
if (!player) {
return true;
} // No player, collision is effectively true
var playerRadius = player.width / 2; // Assuming player is a circle
// Create a hypothetical bounding box for the player at the target position
var playerBounds = {
left: targetX - playerRadius,
right: targetX + playerRadius,
top: targetY - playerRadius,
bottom: targetY + playerRadius
};
for (var i = 0; i < walls.length; i++) {
var wall = walls[i];
var wallBounds = {
left: wall.x,
right: wall.x + wall.wallWidth,
// Use stored dimensions
top: wall.y,
bottom: wall.y + wall.wallHeight // Use stored dimensions
};
// Basic AABB collision check
if (playerBounds.right > wallBounds.left && playerBounds.left < wallBounds.right && playerBounds.bottom > wallBounds.top && playerBounds.top < wallBounds.bottom) {
return true; // Collision detected
}
}
return false; // No collision
}
// Updates the score display
function updateScoreDisplay() {
scoreTxt.setText("Coins: " + score + "/" + requiredCoinsPerLevel);
}
// Updates the timer display
function updateTimerDisplay() {
timerTxt.setText("Time: " + timeLeft);
}
// Timer tick function
function handleTimerTick() {
if (levelComplete) {
return;
} // Stop timer if level is done
timeLeft--;
updateTimerDisplay();
if (timeLeft <= 0) {
LK.clearInterval(timerInterval);
timerInterval = null;
LK.showGameOver(); // LK handles the game over state
}
}
// Starts the level timer
function startTimer() {
if (timerInterval) {
LK.clearInterval(timerInterval);
}
timeLeft = levelTimeLimit; // Reset time
updateTimerDisplay();
timerInterval = LK.setInterval(handleTimerTick, 1000);
}
// Stops the level timer
function stopTimer() {
if (timerInterval) {
LK.clearInterval(timerInterval);
timerInterval = null;
}
}
// --- GUI Setup ---
// Score display
scoreTxt = new Text2('Coins: 0/' + requiredCoinsPerLevel, {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Anchor middle-top
LK.gui.top.addChild(scoreTxt); // Add to top-center GUI area
scoreTxt.y = 20; // Add some padding from the top edge
// Timer display
timerTxt = new Text2('Time: ' + levelTimeLimit, {
size: 60,
fill: 0xFFFFFF
});
timerTxt.anchor.set(1, 0); // Anchor top-right
LK.gui.topRight.addChild(timerTxt); // Add to top-right GUI area
timerTxt.y = 20; // Match score padding
timerTxt.x = -20; // Add some padding from the right edge
// --- Event Handlers ---
game.down = function (x, y, obj) {
if (levelComplete) {
// If level is complete, tap anywhere to proceed
currentLevelIndex++;
if (currentLevelIndex < totalLevels) {
loadLevel(currentLevelIndex);
} else {
LK.showYouWin(); // Completed all levels
}
return; // Don't process drag start
}
// Check if the press is on the player
var playerPos = player.position;
var dx = x - playerPos.x;
var dy = y - playerPos.y;
var distSq = dx * dx + dy * dy;
var playerRadius = player.width / 2;
// Allow starting drag if touching the player
// Add a small tolerance for easier touch
if (distSq < playerRadius * playerRadius * 1.5 * 1.5) {
isDragging = true;
// Calculate offset from player center to touch point
var localPos = game.toLocal(obj.parent.toGlobal(obj.position)); // Get position relative to game stage
dragOffset.x = player.x - localPos.x;
dragOffset.y = player.y - localPos.y;
} else {
isDragging = false;
}
};
game.move = function (x, y, obj) {
if (!isDragging || levelComplete || !player) {
return;
}
var localPos = game.toLocal(obj.parent.toGlobal(obj.position)); // Get position relative to game stage
var targetX = localPos.x + dragOffset.x;
var targetY = localPos.y + dragOffset.y;
// Check for collisions *before* moving
if (!checkWallCollision(targetX, targetY)) {
player.x = targetX;
player.y = targetY;
} else {
// Optional: Try moving only horizontally or vertically if diagonal move fails
// Check X movement only
if (!checkWallCollision(targetX, player.y)) {
player.x = targetX;
}
// Check Y movement only
if (!checkWallCollision(player.x, targetY)) {
player.y = targetY;
}
// If both fail, the player stays put
}
};
game.up = function (x, y, obj) {
isDragging = false;
};
// --- Game Update Loop ---
game.update = function () {
if (levelComplete || !player) {
return;
} // Don't update if level is done or no player
// Check for coin collection
var playerRadius = player.width / 2;
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
var dx = player.x - coin.x;
var dy = player.y - coin.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var coinRadius = coin.width / 2;
if (distance < playerRadius + coinRadius) {
// Collision detected - collect coin
score++;
updateScoreDisplay();
coin.destroy();
coins.splice(i, 1);
// Check for level completion
if (score >= requiredCoinsPerLevel) {
levelComplete = true;
stopTimer();
// Optionally show a "Level Clear! Tap to continue" message
// For now, just rely on the game.down handler checking levelComplete
console.log("Level " + (currentLevelIndex + 1) + " Complete!");
// Note: LK engine might handle transitions, but the prompt asks for tap to continue.
if (currentLevelIndex + 1 >= totalLevels) {
// If this was the last level according to totalLevels count
LK.showYouWin();
}
// Otherwise, wait for tap in game.down
break; // Exit loop after collecting a coin and potentially completing level
}
}
}
};
// --- Start Game ---
loadLevel(currentLevelIndex); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,503 @@
-/****
+/****
+* Classes
+****/
+// Coin class
+var Coin = Container.expand(function () {
+ var self = Container.call(this);
+ var graphics = self.attachAsset('coin', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.rotationSpeed = 0.05; // Radians per frame
+ self.update = function () {
+ self.rotation += self.rotationSpeed;
+ };
+ return self;
+});
+// Base size, will be scaled in code
+// No plugins needed for this version. Tween could be used for rotation, but manual rotation is sufficient.
+// var tween = LK.import('@upit/tween.v1');
+// Player class
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var graphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // No update needed here as movement is handled in the main game loop
+ return self;
+});
+// Wall class
+var Wall = Container.expand(function (width, height) {
+ var self = Container.call(this);
+ // We get a base wall asset and scale it to the desired dimensions
+ var graphics = self.attachAsset('wall', {
+ anchorX: 0.0,
+ // Anchor top-left for easier positioning and scaling
+ anchorY: 0.0,
+ width: width,
+ // Set the actual width
+ height: height // Set the actual height
+ });
+ // Store dimensions for collision checks
+ self.wallWidth = width;
+ self.wallHeight = height;
+ // No update needed for static walls
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x111111 // Dark background
+});
+
+/****
+* Game Code
+****/
+// Define assets used in the game. These will be automatically created and loaded.
+// Game constants and variables
+var gameWidth = 2048;
+var gameHeight = 2732;
+var levelTimeLimit = 60; // Seconds
+var totalLevels = 10; // As per description
+var requiredCoinsPerLevel = 10;
+// Maze definitions (simplified for MVP - add more levels later)
+// Each level: walls array [{x, y, width, height}], coins array [{x, y}], startPos {x, y}
+var mazeLevels = [
+// Level 1 (Simple Box)
+{
+ walls: [{
+ x: 100,
+ y: 200,
+ width: 1848,
+ height: 50
+ },
+ // Top border
+ {
+ x: 100,
+ y: 2482,
+ width: 1848,
+ height: 50
+ },
+ // Bottom border
+ {
+ x: 100,
+ y: 250,
+ width: 50,
+ height: 2232
+ },
+ // Left border
+ {
+ x: 1898,
+ y: 250,
+ width: 50,
+ height: 2232
+ },
+ // Right border
+ {
+ x: 500,
+ y: 800,
+ width: 1000,
+ height: 50
+ } // Internal horizontal
+ ],
+ coins: [{
+ x: 300,
+ y: 400
+ }, {
+ x: 600,
+ y: 600
+ }, {
+ x: 900,
+ y: 400
+ }, {
+ x: 1200,
+ y: 600
+ }, {
+ x: 1500,
+ y: 400
+ }, {
+ x: 300,
+ y: 1200
+ }, {
+ x: 600,
+ y: 1000
+ }, {
+ x: 900,
+ y: 1200
+ }, {
+ x: 1200,
+ y: 1000
+ }, {
+ x: 1500,
+ y: 1200
+ }],
+ startPos: {
+ x: 200,
+ y: 350
+ }
+},
+// Level 2 (More walls) - TODO: Add 8 more levels
+{
+ walls: [{
+ x: 100,
+ y: 200,
+ width: 1848,
+ height: 50
+ },
+ // Top
+ {
+ x: 100,
+ y: 2482,
+ width: 1848,
+ height: 50
+ },
+ // Bottom
+ {
+ x: 100,
+ y: 250,
+ width: 50,
+ height: 2232
+ },
+ // Left
+ {
+ x: 1898,
+ y: 250,
+ width: 50,
+ height: 2232
+ },
+ // Right
+ {
+ x: 100,
+ y: 700,
+ width: 800,
+ height: 50
+ },
+ // H-Wall 1
+ {
+ x: 1148,
+ y: 700,
+ width: 800,
+ height: 50
+ },
+ // H-Wall 2
+ {
+ x: 500,
+ y: 1200,
+ width: 1048,
+ height: 50
+ },
+ // H-Wall 3
+ {
+ x: 999,
+ y: 250,
+ width: 50,
+ height: 1500
+ },
+ // V-Wall 1
+ {
+ x: 500,
+ y: 1600,
+ width: 50,
+ height: 882
+ } // V-Wall 2
+ ],
+ coins: [{
+ x: 250,
+ y: 350
+ }, {
+ x: 1700,
+ y: 350
+ }, {
+ x: 500,
+ y: 500
+ }, {
+ x: 1500,
+ y: 500
+ }, {
+ x: 250,
+ y: 1000
+ }, {
+ x: 1700,
+ y: 1000
+ }, {
+ x: 750,
+ y: 1500
+ }, {
+ x: 1250,
+ y: 1500
+ }, {
+ x: 300,
+ y: 2000
+ }, {
+ x: 1700,
+ y: 2000
+ }],
+ startPos: {
+ x: 200,
+ y: 350
+ }
+}
+// NOTE: Circular mazes are hard to represent with rectangular walls.
+// This implementation uses only rectangular walls.
+];
+// Game state variables
+var currentLevelIndex = 0;
+var walls = [];
+var coins = [];
+var player = null;
+var score = 0;
+var timeLeft = levelTimeLimit;
+var timerInterval = null;
+var scoreTxt = null;
+var timerTxt = null;
+var levelComplete = false;
+var isDragging = false;
+var dragOffset = {
+ x: 0,
+ y: 0
+}; // Offset between touch point and player center
+// --- Helper Functions ---
+// Clears current level elements
+function clearLevel() {
+ walls.forEach(function (wall) {
+ wall.destroy();
+ });
+ walls = [];
+ coins.forEach(function (coin) {
+ coin.destroy();
+ });
+ coins = [];
+ if (player) {
+ player.destroy();
+ player = null;
+ }
+ levelComplete = false;
+ isDragging = false;
+}
+// Loads a level by index
+function loadLevel(levelIndex) {
+ clearLevel();
+ if (levelIndex >= mazeLevels.length) {
+ // Handle case where we run out of predefined levels but haven't reached totalLevels
+ console.log("Warning: Ran out of predefined maze data, repeating last level.");
+ levelIndex = mazeLevels.length - 1; // Repeat the last available level
+ // Ideally, generate levels procedurally or add more data.
+ if (levelIndex < 0) {
+ // Should not happen if mazeLevels has data
+ LK.showGameOver(); // No levels defined at all
+ return;
+ }
+ }
+ currentLevelIndex = levelIndex; // Update the global index tracker
+ var levelData = mazeLevels[levelIndex];
+ // Create walls
+ levelData.walls.forEach(function (wallData) {
+ var wall = new Wall(wallData.width, wallData.height);
+ wall.x = wallData.x;
+ wall.y = wallData.y;
+ game.addChild(wall);
+ walls.push(wall);
+ });
+ // Create coins
+ levelData.coins.forEach(function (coinData) {
+ var coin = new Coin();
+ coin.x = coinData.x;
+ coin.y = coinData.y;
+ game.addChild(coin);
+ coins.push(coin);
+ });
+ // Create player
+ player = new Player();
+ player.x = levelData.startPos.x;
+ player.y = levelData.startPos.y;
+ game.addChild(player);
+ // Reset score and timer for the new level
+ score = 0;
+ timeLeft = levelTimeLimit;
+ updateScoreDisplay();
+ updateTimerDisplay();
+ startTimer();
+}
+// Checks collision between player (at potential new position) and all walls
+function checkWallCollision(targetX, targetY) {
+ if (!player) {
+ return true;
+ } // No player, collision is effectively true
+ var playerRadius = player.width / 2; // Assuming player is a circle
+ // Create a hypothetical bounding box for the player at the target position
+ var playerBounds = {
+ left: targetX - playerRadius,
+ right: targetX + playerRadius,
+ top: targetY - playerRadius,
+ bottom: targetY + playerRadius
+ };
+ for (var i = 0; i < walls.length; i++) {
+ var wall = walls[i];
+ var wallBounds = {
+ left: wall.x,
+ right: wall.x + wall.wallWidth,
+ // Use stored dimensions
+ top: wall.y,
+ bottom: wall.y + wall.wallHeight // Use stored dimensions
+ };
+ // Basic AABB collision check
+ if (playerBounds.right > wallBounds.left && playerBounds.left < wallBounds.right && playerBounds.bottom > wallBounds.top && playerBounds.top < wallBounds.bottom) {
+ return true; // Collision detected
+ }
+ }
+ return false; // No collision
+}
+// Updates the score display
+function updateScoreDisplay() {
+ scoreTxt.setText("Coins: " + score + "/" + requiredCoinsPerLevel);
+}
+// Updates the timer display
+function updateTimerDisplay() {
+ timerTxt.setText("Time: " + timeLeft);
+}
+// Timer tick function
+function handleTimerTick() {
+ if (levelComplete) {
+ return;
+ } // Stop timer if level is done
+ timeLeft--;
+ updateTimerDisplay();
+ if (timeLeft <= 0) {
+ LK.clearInterval(timerInterval);
+ timerInterval = null;
+ LK.showGameOver(); // LK handles the game over state
+ }
+}
+// Starts the level timer
+function startTimer() {
+ if (timerInterval) {
+ LK.clearInterval(timerInterval);
+ }
+ timeLeft = levelTimeLimit; // Reset time
+ updateTimerDisplay();
+ timerInterval = LK.setInterval(handleTimerTick, 1000);
+}
+// Stops the level timer
+function stopTimer() {
+ if (timerInterval) {
+ LK.clearInterval(timerInterval);
+ timerInterval = null;
+ }
+}
+// --- GUI Setup ---
+// Score display
+scoreTxt = new Text2('Coins: 0/' + requiredCoinsPerLevel, {
+ size: 60,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0); // Anchor middle-top
+LK.gui.top.addChild(scoreTxt); // Add to top-center GUI area
+scoreTxt.y = 20; // Add some padding from the top edge
+// Timer display
+timerTxt = new Text2('Time: ' + levelTimeLimit, {
+ size: 60,
+ fill: 0xFFFFFF
+});
+timerTxt.anchor.set(1, 0); // Anchor top-right
+LK.gui.topRight.addChild(timerTxt); // Add to top-right GUI area
+timerTxt.y = 20; // Match score padding
+timerTxt.x = -20; // Add some padding from the right edge
+// --- Event Handlers ---
+game.down = function (x, y, obj) {
+ if (levelComplete) {
+ // If level is complete, tap anywhere to proceed
+ currentLevelIndex++;
+ if (currentLevelIndex < totalLevels) {
+ loadLevel(currentLevelIndex);
+ } else {
+ LK.showYouWin(); // Completed all levels
+ }
+ return; // Don't process drag start
+ }
+ // Check if the press is on the player
+ var playerPos = player.position;
+ var dx = x - playerPos.x;
+ var dy = y - playerPos.y;
+ var distSq = dx * dx + dy * dy;
+ var playerRadius = player.width / 2;
+ // Allow starting drag if touching the player
+ // Add a small tolerance for easier touch
+ if (distSq < playerRadius * playerRadius * 1.5 * 1.5) {
+ isDragging = true;
+ // Calculate offset from player center to touch point
+ var localPos = game.toLocal(obj.parent.toGlobal(obj.position)); // Get position relative to game stage
+ dragOffset.x = player.x - localPos.x;
+ dragOffset.y = player.y - localPos.y;
+ } else {
+ isDragging = false;
+ }
+};
+game.move = function (x, y, obj) {
+ if (!isDragging || levelComplete || !player) {
+ return;
+ }
+ var localPos = game.toLocal(obj.parent.toGlobal(obj.position)); // Get position relative to game stage
+ var targetX = localPos.x + dragOffset.x;
+ var targetY = localPos.y + dragOffset.y;
+ // Check for collisions *before* moving
+ if (!checkWallCollision(targetX, targetY)) {
+ player.x = targetX;
+ player.y = targetY;
+ } else {
+ // Optional: Try moving only horizontally or vertically if diagonal move fails
+ // Check X movement only
+ if (!checkWallCollision(targetX, player.y)) {
+ player.x = targetX;
+ }
+ // Check Y movement only
+ if (!checkWallCollision(player.x, targetY)) {
+ player.y = targetY;
+ }
+ // If both fail, the player stays put
+ }
+};
+game.up = function (x, y, obj) {
+ isDragging = false;
+};
+// --- Game Update Loop ---
+game.update = function () {
+ if (levelComplete || !player) {
+ return;
+ } // Don't update if level is done or no player
+ // Check for coin collection
+ var playerRadius = player.width / 2;
+ for (var i = coins.length - 1; i >= 0; i--) {
+ var coin = coins[i];
+ var dx = player.x - coin.x;
+ var dy = player.y - coin.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ var coinRadius = coin.width / 2;
+ if (distance < playerRadius + coinRadius) {
+ // Collision detected - collect coin
+ score++;
+ updateScoreDisplay();
+ coin.destroy();
+ coins.splice(i, 1);
+ // Check for level completion
+ if (score >= requiredCoinsPerLevel) {
+ levelComplete = true;
+ stopTimer();
+ // Optionally show a "Level Clear! Tap to continue" message
+ // For now, just rely on the game.down handler checking levelComplete
+ console.log("Level " + (currentLevelIndex + 1) + " Complete!");
+ // Note: LK engine might handle transitions, but the prompt asks for tap to continue.
+ if (currentLevelIndex + 1 >= totalLevels) {
+ // If this was the last level according to totalLevels count
+ LK.showYouWin();
+ }
+ // Otherwise, wait for tap in game.down
+ break; // Exit loop after collecting a coin and potentially completing level
+ }
+ }
+ }
+};
+// --- Start Game ---
+loadLevel(currentLevelIndex);
\ No newline at end of file