/**** * 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.update = function () { // Enemy update logic here }; self.shoot = function () { // Enemy shoot logic here }; }); // EnemyBullet class var EnemyBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('enemyBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; 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.update = function () { // Hero update logic here }; self.shoot = function () { // Hero shoot logic here }; }); // 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 enemies = []; var heroBullets = []; var enemyBullets = []; // Create enemies for (var i = 0; i < 5; i++) { var enemy = new Enemy(); enemy.x = 200 + i * 300; // Spread out horizontally enemy.y = 200; // Position towards the top enemies.push(enemy); game.addChild(enemy); } // Touch event to move hero game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); hero.x = pos.x; }); // Shooting logic LK.setInterval(function () { var bullet = new HeroBullet(); bullet.x = hero.x; bullet.y = hero.y - 30; // Start just above the hero heroBullets.push(bullet); game.addChild(bullet); }, 500); // Enemy shooting logic LK.setInterval(function () { enemies.forEach(function (enemy) { var bullet = new EnemyBullet(); bullet.x = enemy.x; bullet.y = enemy.y + 30; // Start just below the enemy enemyBullets.push(bullet); game.addChild(bullet); }); }, 2000); // Game tick LK.on('tick', function () { // Move bullets heroBullets.forEach(function (bullet, index) { bullet.move(); if (bullet.y < 0) { // Remove if off-screen bullet.destroy(); heroBullets.splice(index, 1); } }); enemyBullets.forEach(function (bullet, index) { bullet.move(); if (bullet.y > 2732) { // Remove if off-screen bullet.destroy(); enemyBullets.splice(index, 1); } }); // Collision detection (simplified) enemyBullets.forEach(function (bullet) { if (bullet.intersects(hero)) { // Game over logic here LK.showGameOver(); } }); });
/****
* 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.update = function () {
// Enemy update logic here
};
self.shoot = function () {
// Enemy shoot logic here
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
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.update = function () {
// Hero update logic here
};
self.shoot = function () {
// Hero shoot logic here
};
});
// 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 enemies = [];
var heroBullets = [];
var enemyBullets = [];
// Create enemies
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = 200 + i * 300; // Spread out horizontally
enemy.y = 200; // Position towards the top
enemies.push(enemy);
game.addChild(enemy);
}
// Touch event to move hero
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.x = pos.x;
});
// Shooting logic
LK.setInterval(function () {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y - 30; // Start just above the hero
heroBullets.push(bullet);
game.addChild(bullet);
}, 500);
// Enemy shooting logic
LK.setInterval(function () {
enemies.forEach(function (enemy) {
var bullet = new EnemyBullet();
bullet.x = enemy.x;
bullet.y = enemy.y + 30; // Start just below the enemy
enemyBullets.push(bullet);
game.addChild(bullet);
});
}, 2000);
// Game tick
LK.on('tick', function () {
// Move bullets
heroBullets.forEach(function (bullet, index) {
bullet.move();
if (bullet.y < 0) {
// Remove if off-screen
bullet.destroy();
heroBullets.splice(index, 1);
}
});
enemyBullets.forEach(function (bullet, index) {
bullet.move();
if (bullet.y > 2732) {
// Remove if off-screen
bullet.destroy();
enemyBullets.splice(index, 1);
}
});
// Collision detection (simplified)
enemyBullets.forEach(function (bullet) {
if (bullet.intersects(hero)) {
// Game over logic here
LK.showGameOver();
}
});
});