/****
* Classes
****/
var AutoClickItem = Container.expand(function () {
var self = Container.call(this);
var itemGraphics = self.attachAsset('text', {
anchorX: 0.5,
anchorY: 0.5,
text: 'Auto Click - 5 Points',
size: 50,
fill: '#ffffff'
});
self.cost = 5;
self.on('down', function () {
if (score >= self.cost) {
updateScore(-self.cost);
activateAutoClick();
}
});
});
// Bubble class
var Bubble = Container.expand(function () {
var self = Container.call(this);
var bubbleGraphics = self.attachAsset('bubble', {
anchorX: 0.5,
anchorY: 0.5
});
// Add event listener for popping bubble
self.on('down', function () {
updateScore(1); // Increase score by 1 when bubble is clicked
// Start pop effect
LK.effects.flashObject(self, 0xffffff, 300, function () {
self.destroy();
}); // Flash the bubble and then remove it from the game
});
// Set bubble properties
self.speed = Math.random() * 2 + 1; // Random speed between 1 and 3
self.direction = Math.random() * Math.PI * 2; // Random direction
// Move bubble
self.move = function () {
self.x += Math.cos(self.direction) * self.speed;
self.y += Math.sin(self.direction) * self.speed;
// Set a lifespan for the bubble (60 seconds)
self.lifespan = 60 * 60;
// Decrease lifespan each tick
self.lifespan--;
// Destroy the bubble when lifespan reaches 0
if (self.lifespan <= 0) {
self.destroy();
}
};
// Check if bubble is out of bounds
self.isOutOfBounds = function () {
return self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732;
};
});
/****
* Initialize Game
****/
// Create a GUI button to open the shop
// Add the shop button to the GUI
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize bubble asset
// Initialize bubbles array
function activateAutoClick() {
// Create an auto-click effect that simulates bubble popping every second
LK.setInterval(function () {
if (bubbles.length > 0) {
popBubble(bubbles[0], 0);
}
}, 1000);
}
var bubbles = [];
// Create a score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Score variable
var score = 0;
// Function to update score
function updateScore(value) {
score += value;
scoreTxt.setText(score.toString());
}
// Function to create a new bubble
function createBubble() {
var bubble = new Bubble();
// Random position within the game area
bubble.x = Math.random() * 2048;
bubble.y = Math.random() * 2732;
bubbles.push(bubble);
game.addChild(bubble);
}
// Function to handle bubble popping
function popBubble(bubble, index) {
updateScore(1); // Increase score
// Start pop effect and then remove bubble
LK.effects.flashObject(bubble, 0xffffff, 300, function () {
bubble.destroy();
});
bubbles.splice(index, 1); // Remove bubble from array
}
// Game tick event
LK.on('tick', function () {
// Move bubbles
for (var i = bubbles.length - 1; i >= 0; i--) {
bubbles[i].move();
// Check if bubble is out of bounds
if (bubbles[i].isOutOfBounds()) {
bubbles[i].destroy(); // Remove bubble from game
bubbles.splice(i, 1); // Remove bubble from array
}
}
// Create new bubble every 60 frames (1 second)
if (LK.ticks % 60 === 0) {
createBubble();
}
});