/****
* Classes
****/
// Box class: represents a clickable box that appears randomly on the screen
var Box = Container.expand(function () {
var self = Container.call(this);
// Attach a box asset, centered
var boxAsset = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
// Track lastX, lastY for event transitions
self.lastX = 0;
self.lastY = 0;
self.lastWasDown = false;
// Called when the box is pressed/touched
self.down = function (x, y, obj) {
if (!self.lastWasDown) {
self.lastWasDown = true;
// Notify game that this box was clicked
if (typeof onBoxClicked === "function") {
onBoxClicked(self);
}
}
};
// Called when the box is released
self.up = function (x, y, obj) {
self.lastWasDown = false;
};
// Called every tick
self.update = function () {
// No movement, but could add animations here
};
return self;
});
/****
* Initialize Game
****/
// Core game variables
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Core game variables
var currentBox = null; // The currently visible box
var score = 0; // Player's score
var misses = 0; // Number of missed boxes
var boxLifetime = 1200; // ms, how long a box stays visible (decreases with difficulty)
var minBoxLifetime = 500; // ms, minimum box lifetime
var boxTimer = null; // Timer for box disappearance
var scoreTxt = null; // Score display
var missLimit = 3; // Number of misses before game over
// Function to handle box click (defined in Game Code)
var onBoxClicked = null;
// Utility: get a random position for the box, avoiding the top-left 100x100 area
function getRandomBoxPosition(boxWidth, boxHeight) {
var margin = 100;
var minX = margin + boxWidth / 2;
var maxX = 2048 - boxWidth / 2;
var minY = margin + boxHeight / 2;
var maxY = 2732 - boxHeight / 2;
var x = minX + Math.random() * (maxX - minX);
var y = minY + Math.random() * (maxY - minY);
return {
x: x,
y: y
};
}
// Show a new box at a random position
function spawnBox() {
// Remove previous box if exists
if (currentBox) {
currentBox.destroy();
currentBox = null;
}
// Create new box
var box = new Box();
var boxW = box.width;
var boxH = box.height;
var pos = getRandomBoxPosition(boxW, boxH);
box.x = pos.x;
box.y = pos.y;
game.addChild(box);
currentBox = box;
// Set up timer for box disappearance
if (boxTimer) {
LK.clearTimeout(boxTimer);
}
boxTimer = LK.setTimeout(function () {
// Missed the box
if (currentBox === box) {
handleBoxMiss();
}
}, boxLifetime);
}
// Handle when a box is clicked
onBoxClicked = function onBoxClicked(box) {
if (currentBox !== box) return; // Only count if it's the current box
score += 1;
updateScoreText();
// Increase difficulty: decrease box lifetime, but not below minimum
boxLifetime = Math.max(minBoxLifetime, boxLifetime - 30);
// Remove box and spawn a new one
if (boxTimer) {
LK.clearTimeout(boxTimer);
boxTimer = null;
}
if (currentBox) {
currentBox.destroy();
currentBox = null;
}
spawnBox();
};
// Handle when a box is missed (not clicked in time)
function handleBoxMiss() {
misses += 1;
if (currentBox) {
currentBox.destroy();
currentBox = null;
}
if (boxTimer) {
LK.clearTimeout(boxTimer);
boxTimer = null;
}
if (misses >= missLimit) {
// Game over
LK.showGameOver();
return;
}
// Show next box
spawnBox();
}
// Update the score display
function updateScoreText() {
if (scoreTxt) {
scoreTxt.setText(score + "");
}
}
// Initialize score text UI
scoreTxt = new Text2("0", {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Start the game by spawning the first box
spawnBox(); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,151 @@
-/****
+/****
+* Classes
+****/
+// Box class: represents a clickable box that appears randomly on the screen
+var Box = Container.expand(function () {
+ var self = Container.call(this);
+ // Attach a box asset, centered
+ var boxAsset = self.attachAsset('box', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Track lastX, lastY for event transitions
+ self.lastX = 0;
+ self.lastY = 0;
+ self.lastWasDown = false;
+ // Called when the box is pressed/touched
+ self.down = function (x, y, obj) {
+ if (!self.lastWasDown) {
+ self.lastWasDown = true;
+ // Notify game that this box was clicked
+ if (typeof onBoxClicked === "function") {
+ onBoxClicked(self);
+ }
+ }
+ };
+ // Called when the box is released
+ self.up = function (x, y, obj) {
+ self.lastWasDown = false;
+ };
+ // Called every tick
+ self.update = function () {
+ // No movement, but could add animations here
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
+// Core game variables
var game = new LK.Game({
backgroundColor: 0x000000
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+// Core game variables
+var currentBox = null; // The currently visible box
+var score = 0; // Player's score
+var misses = 0; // Number of missed boxes
+var boxLifetime = 1200; // ms, how long a box stays visible (decreases with difficulty)
+var minBoxLifetime = 500; // ms, minimum box lifetime
+var boxTimer = null; // Timer for box disappearance
+var scoreTxt = null; // Score display
+var missLimit = 3; // Number of misses before game over
+// Function to handle box click (defined in Game Code)
+var onBoxClicked = null;
+// Utility: get a random position for the box, avoiding the top-left 100x100 area
+function getRandomBoxPosition(boxWidth, boxHeight) {
+ var margin = 100;
+ var minX = margin + boxWidth / 2;
+ var maxX = 2048 - boxWidth / 2;
+ var minY = margin + boxHeight / 2;
+ var maxY = 2732 - boxHeight / 2;
+ var x = minX + Math.random() * (maxX - minX);
+ var y = minY + Math.random() * (maxY - minY);
+ return {
+ x: x,
+ y: y
+ };
+}
+// Show a new box at a random position
+function spawnBox() {
+ // Remove previous box if exists
+ if (currentBox) {
+ currentBox.destroy();
+ currentBox = null;
+ }
+ // Create new box
+ var box = new Box();
+ var boxW = box.width;
+ var boxH = box.height;
+ var pos = getRandomBoxPosition(boxW, boxH);
+ box.x = pos.x;
+ box.y = pos.y;
+ game.addChild(box);
+ currentBox = box;
+ // Set up timer for box disappearance
+ if (boxTimer) {
+ LK.clearTimeout(boxTimer);
+ }
+ boxTimer = LK.setTimeout(function () {
+ // Missed the box
+ if (currentBox === box) {
+ handleBoxMiss();
+ }
+ }, boxLifetime);
+}
+// Handle when a box is clicked
+onBoxClicked = function onBoxClicked(box) {
+ if (currentBox !== box) return; // Only count if it's the current box
+ score += 1;
+ updateScoreText();
+ // Increase difficulty: decrease box lifetime, but not below minimum
+ boxLifetime = Math.max(minBoxLifetime, boxLifetime - 30);
+ // Remove box and spawn a new one
+ if (boxTimer) {
+ LK.clearTimeout(boxTimer);
+ boxTimer = null;
+ }
+ if (currentBox) {
+ currentBox.destroy();
+ currentBox = null;
+ }
+ spawnBox();
+};
+// Handle when a box is missed (not clicked in time)
+function handleBoxMiss() {
+ misses += 1;
+ if (currentBox) {
+ currentBox.destroy();
+ currentBox = null;
+ }
+ if (boxTimer) {
+ LK.clearTimeout(boxTimer);
+ boxTimer = null;
+ }
+ if (misses >= missLimit) {
+ // Game over
+ LK.showGameOver();
+ return;
+ }
+ // Show next box
+ spawnBox();
+}
+// Update the score display
+function updateScoreText() {
+ if (scoreTxt) {
+ scoreTxt.setText(score + "");
+ }
+}
+// Initialize score text UI
+scoreTxt = new Text2("0", {
+ size: 150,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+// Start the game by spawning the first box
+spawnBox();
\ No newline at end of file