/****
* Classes
****/
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.move = function (direction) {
if (direction === 'up' && self.y > 0) {
self.y -= self.speed;
} else if (direction === 'down' && self.y < 2732 - playerGraphics.height) {
self.y += self.speed;
}
};
});
// Target class
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.isVisible = false;
self.show = function () {
self.isVisible = true;
self.alpha = 1;
LK.setTimeout(function () {
self.hide();
}, 1000);
};
self.hide = function () {
self.isVisible = false;
self.alpha = 0;
};
self.resetPosition = function () {
var position;
var overlap;
do {
position = {
x: Math.random() * (2048 - targetGraphics.width) + targetGraphics.width / 2,
y: Math.random() * (2732 - targetGraphics.height) + targetGraphics.height / 2
};
overlap = targets.some(function (other) {
return other !== self && Math.abs(other.x - position.x) < targetGraphics.width && Math.abs(other.y - position.y) < targetGraphics.height;
});
} while (overlap);
self.x = position.x;
self.y = position.y;
};
// Initialize with random position
self.resetPosition();
});
/****
* Initialize Game
****/
// Initialize game variables
var game = new LK.Game({
backgroundColor: 0x000000,
// Init game with black background
gameOver: false,
elapsedTime: 0
});
/****
* Game Code
****/
// Set a random background image
var backgroundImages = ['imageId1', 'imageId2', 'imageId3']; // Replace with actual image IDs
var randomIndex = Math.floor(Math.random() * backgroundImages.length);
var backgroundImage = LK.getAsset(backgroundImages[randomIndex], {
width: 2048,
height: 2732
});
game.addChild(backgroundImage);
// Initialize players and ball
// Define the assets for the players and the ball
// Update the elapsed time every tick
var player1 = game.addChild(new Player());
// Initialize an array of targets
var targets = [];
for (var i = 0; i < 10; i++) {
var target = new Target();
game.addChild(target);
targets.push(target);
}
// Set initial positions
player1.x = 2048 / 2;
player1.y = 100;
// Game variables
var visibilityThreshold = 500; // Distance at which the ball becomes visible
var ballVisibilityCounter = 0; // Counter to track ball visibility
// Touch event handlers
function handlePlayerTouch(obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
targets.forEach(function (target, index) {
if (target.isVisible && pos.x >= target.x - target.width / 2 && pos.x <= target.x + target.width / 2 && pos.y >= target.y - target.height / 2 && pos.y <= target.y + target.height / 2) {
LK.setScore(LK.getScore() + 1);
var eliminatedText = new Text2('eliminated', {
size: 50,
fill: '#ffffff',
anchorX: 0.5,
anchorY: 0.5
});
eliminatedText.x = target.x;
eliminatedText.y = target.y;
game.addChild(eliminatedText);
LK.setTimeout(function () {
eliminatedText.destroy();
}, 2000);
target.destroy();
targets.splice(index, 1);
}
});
}
// Add touch event listener to the game
game.on('down', handlePlayerTouch);
// Game tick event
LK.on('tick', function () {
// Handle target appearance and disappearance
if (ballVisibilityCounter < visibilityThreshold) {
ballVisibilityCounter++;
} else if (ballVisibilityCounter === visibilityThreshold) {
ballVisibilityCounter = 0; // Reset counter to start the hide/show cycle
// Show targets for 1 second then hide them and wait for 5 seconds
targets.forEach(function (target) {
if (!target.isVisible) {
target.show();
}
});
LK.setTimeout(function () {
targets.forEach(function (target) {
target.hide();
});
// After targets are hidden, wait for 5 seconds before showing them again
LK.setTimeout(function () {
targets.forEach(function (target) {
target.resetPosition();
target.show();
});
}, 5000);
}, 1000);
}
// One-minute timer to end the game if not all targets are eliminated
if (!game.gameOver && game.elapsedTime >= 60000) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
game.gameOver = true;
}
});
LK.on('tick', function () {
if (!game.gameOver) {
game.elapsedTime += 1000 / 60;
}
});