/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class representing the poppable balls
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 2 + 1; // Random speed for each ball
// Update function to move the ball
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -ballGraphics.height; // Reset position if it goes off screen
self.x = Math.random() * 2048; // Randomize x position
}
};
// Function to handle popping the ball
self.pop = function () {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
self.y = -ballGraphics.height; // Reset position
self.x = Math.random() * 2048; // Randomize x position
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Array to hold all balls
var balls = [];
// Create initial balls
for (var i = 0; i < 10; i++) {
var ball = new Ball();
ball.x = Math.random() * 2048;
ball.y = Math.random() * 2732;
balls.push(ball);
game.addChild(ball);
}
// Handle touch events to pop balls
game.down = function (x, y, obj) {
balls.forEach(function (ball) {
if (ball.intersects(obj)) {
ball.pop();
}
});
};
// Update function to move balls
game.update = function () {
balls.forEach(function (ball) {
ball.update();
});
};