Code edit (3 edits merged)
Please save this source code
User prompt
place the timer under the score
User prompt
add a start text on top of the button, center it on the button
Code edit (1 edits merged)
Please save this source code
User prompt
if count down is NaN display 30
User prompt
if count down is NaN display 0
User prompt
if the count down hits 0 game over
User prompt
make the timer an text object
User prompt
add a count down from 30 seconds that starts on game start
User prompt
add a start game button
User prompt
fix the timer so that it updates
User prompt
the timers beginning value should be 30 seconds
User prompt
the timer should use the duration of the var gameduration
User prompt
set the timer to 30 seconds
User prompt
the timer should be a text element
User prompt
expand the game
Code edit (1 edits merged)
Please save this source code
Initial prompt
aim trainer v2
===================================================================
--- original.js
+++ change.js
@@ -1,7 +1,25 @@
/****
* Classes
****/
+// StartButton class
+var StartButton = Container.expand(function () {
+ var self = Container.call(this);
+ // Initialize start button with a shape asset
+ var startButtonGraphics = self.attachAsset('startButton', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ shape: 'box',
+ width: 400,
+ height: 200,
+ color: 0x0d6eaf // Blue color
+ });
+ // Method to handle start button click
+ self.on('down', function () {
+ self.destroy(); // Destroy start button after click
+ startGame(); // Start the game
+ });
+});
// Assets will be automatically created based on usage in the code.
// Target class
var Target = Container.expand(function () {
var self = Container.call(this);
@@ -31,31 +49,8 @@
LK.setScore(LK.getScore() + 1); // Increase score
self.destroy(); // Destroy target after click
});
});
-// Timer class
-var Timer = Container.expand(function () {
- var self = Container.call(this);
- // Initialize timer with a text asset
- var timerGraphics = new Text2('30', {
- size: 50,
- fill: "#ffffff"
- });
- self.timeLeft = 30;
- self.addChild(timerGraphics);
- timerGraphics.anchor.set(0.5, 0.5);
- timerGraphics.x = self.width / 2;
- timerGraphics.y = self.height / 2;
- // Method to decrease the timer
- self.tick = function () {
- if (self.timeLeft > 0) {
- self.timeLeft--;
- timerGraphics.text = self.timeLeft.toString();
- } else {
- self.destroy(); // Destroy timer if it's 0
- }
- };
-});
/****
* Initialize Game
****/
@@ -65,24 +60,27 @@
/****
* Game Code
****/
-// Initialize timer
-var timer = new Timer();
-timer.x = 2048 / 2; // Center the timer horizontally
-timer.y = 50; // Position the timer at the top of the screen
-game.addChild(timer); // Add timer to the game
+// Initialize score and targets array
var targets = [];
var gameDuration = 30000; // 30 seconds in milliseconds
var targetSpawnInterval = 500; // 0.5 seconds in milliseconds
-var gameEndTime = Date.now() + gameDuration; // Calculate game end time
+var gameEndTime; // Game end time will be set when the game starts
+var startButton = new StartButton();
+startButton.x = 2048 / 2; // Center the start button
+startButton.y = 2732 / 2;
+game.addChild(startButton);
// Create score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt); // Add score display to the top-center of the screen
// Function to spawn targets
+function startGame() {
+ gameEndTime = Date.now() + gameDuration; // Calculate game end time
+}
function spawnTarget() {
if (Date.now() < gameEndTime) {
// Check if game time is not over
var target = new Target();
@@ -104,14 +102,10 @@
// Check if target has been destroyed
targets.splice(index, 1); // Remove target from array
}
});
- // Update timer every second
- if (LK.ticks % 60 == 0) {
- timer.tick();
- }
- // End game when timer is 0
- if (timer.timeLeft <= 0) {
+ // End game when time is up
+ if (Date.now() >= gameEndTime) {
LK.showGameOver();
}
});
// Spawn targets at intervals