/**** * Classes ****/ // ClickerButton class var ClickerButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('clickerButton', { anchorX: 0.5, anchorY: 0.5 }); self.interactive = true; // Make the button interactive self.buttonMode = true; // Change cursor on hover self.on('down', function () { // Increase score when button is clicked game.score++; scoreText.setText("Score: " + game.score); }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize score // Initialize the clicker button asset game.score = 0; // Create the clicker button and add it to the game var clickerButton = game.addChild(new ClickerButton()); clickerButton.x = game.width / 2; clickerButton.y = game.height / 2; // Create the score text and add it to the GUI var scoreText = new Text2("Score: 0", { size: 100, fill: "#ffffff" }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); scoreText.x = game.width / 2; // Main game update loop LK.on('tick', function () { // Game logic goes here // In this simple clicker game, the score is updated via the button click event });
/****
* Classes
****/
// ClickerButton class
var ClickerButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('clickerButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.interactive = true; // Make the button interactive
self.buttonMode = true; // Change cursor on hover
self.on('down', function () {
// Increase score when button is clicked
game.score++;
scoreText.setText("Score: " + game.score);
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize score
// Initialize the clicker button asset
game.score = 0;
// Create the clicker button and add it to the game
var clickerButton = game.addChild(new ClickerButton());
clickerButton.x = game.width / 2;
clickerButton.y = game.height / 2;
// Create the score text and add it to the GUI
var scoreText = new Text2("Score: 0", {
size: 100,
fill: "#ffffff"
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreText.x = game.width / 2;
// Main game update loop
LK.on('tick', function () {
// Game logic goes here
// In this simple clicker game, the score is updated via the button click event
});