/**** * Classes ****/ // Assets will be automatically created and loaded by the LK engine based on usage in the game code. // Player's Bullet var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y -= self.speed; if (self.y < 0) { self.destroy(); } }; }); // Target var Target = Container.expand(function () { var self = Container.call(this); var targetGraphics = self.attachAsset('target', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Targets just float in space, could add movement or other behaviors here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Initialize game with a black background }); /**** * Game Code ****/ var bullets = []; var targets = []; var score = 0; // Create a target at a random position function spawnTarget() { var target = new Target(); target.x = Math.random() * 2048; target.y = Math.random() * 2732; game.addChild(target); targets.push(target); } // Check for bullet collisions with targets function checkCollisions() { bullets.forEach(function (bullet, bulletIndex) { targets.forEach(function (target, targetIndex) { if (bullet.intersects(target)) { bullet.destroy(); target.destroy(); bullets.splice(bulletIndex, 1); targets.splice(targetIndex, 1); score += 1; LK.effects.flashObject(target, 0xff0000, 500); // Flash target red on hit } }); }); } // Shoot a bullet from the bottom center of the screen function shoot(x, y) { var bullet = new Bullet(); bullet.x = x; bullet.y = 2732; // Start from the bottom of the screen game.addChild(bullet); bullets.push(bullet); } // Game update function game.update = function () { if (LK.ticks % 120 == 0) { // Spawn a target every 2 seconds spawnTarget(); } checkCollisions(); }; // Touch event to shoot game.down = function (x, y, obj) { shoot(x, y); }; // Initialize a few targets for (var i = 0; i < 5; i++) { spawnTarget(); }
/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on usage in the game code.
// Player's Bullet
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y -= self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Target
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Targets just float in space, could add movement or other behaviors here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Initialize game with a black background
});
/****
* Game Code
****/
var bullets = [];
var targets = [];
var score = 0;
// Create a target at a random position
function spawnTarget() {
var target = new Target();
target.x = Math.random() * 2048;
target.y = Math.random() * 2732;
game.addChild(target);
targets.push(target);
}
// Check for bullet collisions with targets
function checkCollisions() {
bullets.forEach(function (bullet, bulletIndex) {
targets.forEach(function (target, targetIndex) {
if (bullet.intersects(target)) {
bullet.destroy();
target.destroy();
bullets.splice(bulletIndex, 1);
targets.splice(targetIndex, 1);
score += 1;
LK.effects.flashObject(target, 0xff0000, 500); // Flash target red on hit
}
});
});
}
// Shoot a bullet from the bottom center of the screen
function shoot(x, y) {
var bullet = new Bullet();
bullet.x = x;
bullet.y = 2732; // Start from the bottom of the screen
game.addChild(bullet);
bullets.push(bullet);
}
// Game update function
game.update = function () {
if (LK.ticks % 120 == 0) {
// Spawn a target every 2 seconds
spawnTarget();
}
checkCollisions();
};
// Touch event to shoot
game.down = function (x, y, obj) {
shoot(x, y);
};
// Initialize a few targets
for (var i = 0; i < 5; i++) {
spawnTarget();
}