/**** * Classes ****/ // Define a Chaser class var Chaser = Container.expand(function () { var self = Container.call(this); var chaserGraphics = self.attachAsset('chaser', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { // Chaser update logic }; }); //<Assets used in the game will automatically appear here> // Define a Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Player update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize timer var elapsedTime = 0; var timerText = new Text2('Time: 0', { size: 100, fill: "#ffffff" }); timerText.anchor.set(0.5, 0); LK.gui.top.addChild(timerText); var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; var chaser1 = game.addChild(new Chaser()); chaser1.x = 100; chaser1.y = 100; var chaser2 = game.addChild(new Chaser()); chaser2.x = 300; chaser2.y = 300; // Handle player movement game.down = function (x, y, obj) { player.x = x; player.y = y; // Update timer if (LK.ticks % 60 === 0) { // Assuming 60 FPS, update every second elapsedTime++; timerText.setText('Time: ' + elapsedTime); } }; // Update game logic game.update = function () { // Move chaser1 randomly chaser1.x += (Math.random() - 0.5) * chaser1.speed; chaser1.y += (Math.random() - 0.5) * chaser1.speed; // Move chaser2 randomly chaser2.x += (Math.random() - 0.5) * chaser2.speed; chaser2.y += (Math.random() - 0.5) * chaser2.speed; // Check for collision with chaser1 if (player.intersects(chaser1)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Check for collision with chaser2 if (player.intersects(chaser2)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } };
===================================================================
--- original.js
+++ change.js
@@ -36,9 +36,16 @@
/****
* Game Code
****/
-// Initialize player and chasers
+// Initialize timer
+var elapsedTime = 0;
+var timerText = new Text2('Time: 0', {
+ size: 100,
+ fill: "#ffffff"
+});
+timerText.anchor.set(0.5, 0);
+LK.gui.top.addChild(timerText);
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
var chaser1 = game.addChild(new Chaser());
@@ -50,8 +57,14 @@
// Handle player movement
game.down = function (x, y, obj) {
player.x = x;
player.y = y;
+ // Update timer
+ if (LK.ticks % 60 === 0) {
+ // Assuming 60 FPS, update every second
+ elapsedTime++;
+ timerText.setText('Time: ' + elapsedTime);
+ }
};
// Update game logic
game.update = function () {
// Move chaser1 randomly