/****
* Classes
****/
// FlyingObject class
var FlyingObject = Container.expand(function () {
var self = Container.call(this);
var objectGraphics = self.attachAsset('flyingObject', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.move = function () {
self.y += self.speed;
// Remove object if it moves off screen
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
// Assets will be automatically created based on usage in the code.
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var flyingObjects = [];
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Function to spawn flying objects
function spawnFlyingObject() {
var flyingObject = new FlyingObject();
flyingObject.x = 2048 / 2;
flyingObject.y = 2732 / 2;
flyingObjects.push(flyingObject);
game.addChild(flyingObject);
}
// Handle slicing action
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
flyingObjects.forEach(function (object, index) {
if (Math.abs(object.x - pos.x) < 100 && Math.abs(object.y - pos.y) < 100) {
object.destroy(); // Destroy the object
flyingObjects.splice(index, 1); // Remove from array
score++; // Increase score
scoreTxt.setText(score.toString()); // Update score display
}
});
});
// Game tick function
LK.on('tick', function () {
// Move flying objects
flyingObjects.forEach(function (object) {
object.move();
});
// Spawn a new flying object every 60 frames (about 1 second)
if (LK.ticks % 60 === 0) {
spawnFlyingObject();
}
});
// Initialize the game with a flying object
spawnFlyingObject(); ===================================================================
--- original.js
+++ change.js
@@ -38,10 +38,10 @@
LK.gui.top.addChild(scoreTxt);
// Function to spawn flying objects
function spawnFlyingObject() {
var flyingObject = new FlyingObject();
- flyingObject.x = 0;
- flyingObject.y = Math.random() * 2732;
+ flyingObject.x = 2048 / 2;
+ flyingObject.y = 2732 / 2;
flyingObjects.push(flyingObject);
game.addChild(flyingObject);
}
// Handle slicing action