===================================================================
--- original.js
+++ change.js
@@ -1,86 +1,122 @@
-/****
+/****
* Classes
-****/
+****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Planet class representing a planet that can be tapped to explode
var Planet = Container.expand(function () {
- var self = Container.call(this);
- var planetGraphics = self.attachAsset('planet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- // Randomize initial position
- self.x = Math.random() * 2048;
- self.y = Math.random() * 2732;
- // Event handler for when the planet is tapped
- self.down = function (x, y, obj) {
- // Trigger explosion effect
- LK.effects.flashObject(self, 0xff0000, 500);
- // Increase score
- LK.setScore(LK.getScore() + 1);
- // Update score text
- scoreTxt.setText(LK.getScore());
- // Destroy the planet
- self.destroy();
- };
- // Update function called every game tick
- self.update = function () {
- // Move the planet downwards
- self.y += 2;
- // Reset position if it goes off screen
- if (self.y > 2732) {
- self.y = -self.height;
- self.x = Math.random() * 2048;
- }
- };
+ var self = Container.call(this);
+ var planetGraphics = self.attachAsset('planet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Randomize initial position
+ self.x = Math.random() * 2048;
+ self.y = Math.random() * 2732;
+ // Event handler for when the planet is tapped
+ self.down = function (x, y, obj) {
+ // Trigger explosion effect
+ LK.effects.flashObject(self, 0xff0000, 500);
+ // Increase score
+ LK.setScore(LK.getScore() + 1);
+ // Update score text
+ scoreTxt.setText(LK.getScore());
+ // Destroy the planet
+ self.destroy();
+ // Check if score is enough for exchange
+ if (LK.getScore() >= 10) {
+ // Exchange score for a task
+ LK.setScore(LK.getScore() - 10);
+ // Add a task to the task bar
+ taskBar.addTask();
+ }
+ };
+ // Update function called every game tick
+ self.update = function () {
+ // Move the planet downwards
+ self.y += 2;
+ // Reset position if it goes off screen
+ if (self.y > 2732) {
+ self.y = -self.height;
+ self.x = Math.random() * 2048;
+ }
+ };
});
+// Task class representing a task that can be completed for score
+var Task = Container.expand(function () {
+ var self = Container.call(this);
+ // Method to complete the task
+ self.complete = function () {
+ // Increase score
+ LK.setScore(LK.getScore() + 5);
+ // Update score text
+ scoreTxt.setText(LK.getScore());
+ // Remove the task from the task bar
+ self.parent.removeChild(self);
+ };
+});
+// TaskBar class representing a task bar that can hold tasks
+var TaskBar = Container.expand(function () {
+ var self = Container.call(this);
+ // Array to hold tasks
+ self.tasks = [];
+ // Method to add a task to the task bar
+ self.addTask = function () {
+ // Create a new task and add it to the task bar
+ var newTask = new Task();
+ self.tasks.push(newTask);
+ self.addChild(newTask);
+ };
+});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Initialize score text
var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+ size: 150,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Array to keep track of planets
var planets = [];
// Function to create a new planet
function createPlanet() {
- var newPlanet = new Planet();
- planets.push(newPlanet);
- game.addChild(newPlanet);
+ var newPlanet = new Planet();
+ planets.push(newPlanet);
+ game.addChild(newPlanet);
}
// Create initial planets
for (var i = 0; i < 5; i++) {
- createPlanet();
+ createPlanet();
}
+// Create the task bar and add it to the game
+var taskBar = new TaskBar();
+game.addChild(taskBar);
// Game update function
game.update = function () {
- // Update all planets
- for (var i = planets.length - 1; i >= 0; i--) {
- planets[i].update();
- }
- // Add new planet every 60 ticks
- if (LK.ticks % 60 == 0) {
- createPlanet();
- }
+ // Update all planets
+ for (var i = planets.length - 1; i >= 0; i--) {
+ planets[i].update();
+ }
+ // Add new planet every 60 ticks
+ if (LK.ticks % 60 == 0) {
+ createPlanet();
+ }
};
// Handle game over when score reaches a certain threshold
function checkGameOver() {
- if (LK.getScore() >= 50) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
+ if (LK.getScore() >= 50) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
}
// Set interval to check for game over condition
LK.setInterval(checkGameOver, 1000);
\ No newline at end of file