/****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.move = function () {
self.y += self.speed;
};
});
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.move = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize assets for the game
var hero = game.addChild(new Hero());
hero.x = 1024; // Center horizontally
hero.y = 2500; // Position towards the bottom
var heroBullets = [];
var enemies = [];
// Function to spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048; // Random horizontal position
enemy.y = 0; // Start from the top
enemies.push(enemy);
game.addChild(enemy);
}
// Function to fire a bullet
function fireBullet() {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
heroBullets.push(bullet);
game.addChild(bullet);
}
// Touch event to move hero and fire bullets
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.move(pos.x, pos.y);
fireBullet();
});
// Game tick event
LK.on('tick', function () {
// Move bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].move();
if (heroBullets[i].y < 0) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Move enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].move();
if (enemies[j].y > 2732) {
enemies[j].destroy();
enemies.splice(j, 1);
}
}
// Collision detection
enemies.forEach(function (enemy, index) {
if (hero.intersects(enemy)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
heroBullets.forEach(function (bullet, bulletIndex) {
if (bullet.intersects(enemy)) {
enemy.destroy();
enemies.splice(index, 1);
bullet.destroy();
heroBullets.splice(bulletIndex, 1);
LK.setScore(LK.getScore() + 1);
}
});
});
// Spawn enemies periodically
if (LK.ticks % 120 == 0) {
spawnEnemy();
}
}); /****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.move = function () {
self.y += self.speed;
};
});
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.move = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize assets for the game
var hero = game.addChild(new Hero());
hero.x = 1024; // Center horizontally
hero.y = 2500; // Position towards the bottom
var heroBullets = [];
var enemies = [];
// Function to spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048; // Random horizontal position
enemy.y = 0; // Start from the top
enemies.push(enemy);
game.addChild(enemy);
}
// Function to fire a bullet
function fireBullet() {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
heroBullets.push(bullet);
game.addChild(bullet);
}
// Touch event to move hero and fire bullets
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.move(pos.x, pos.y);
fireBullet();
});
// Game tick event
LK.on('tick', function () {
// Move bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].move();
if (heroBullets[i].y < 0) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Move enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].move();
if (enemies[j].y > 2732) {
enemies[j].destroy();
enemies.splice(j, 1);
}
}
// Collision detection
enemies.forEach(function (enemy, index) {
if (hero.intersects(enemy)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
heroBullets.forEach(function (bullet, bulletIndex) {
if (bullet.intersects(enemy)) {
enemy.destroy();
enemies.splice(index, 1);
bullet.destroy();
heroBullets.splice(bulletIndex, 1);
LK.setScore(LK.getScore() + 1);
}
});
});
// Spawn enemies periodically
if (LK.ticks % 120 == 0) {
spawnEnemy();
}
});