/****
* 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 = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for hero
};
self.attack = function () {
var newBullet = new HeroBullet();
newBullet.x = self.x;
newBullet.y = self.y - heroGraphics.height / 2;
game.addChild(newBullet);
heroBullets.push(newBullet);
};
});
// 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 = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var hero;
var enemies = [];
var heroBullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Spawn enemies
function spawnEnemy() {
var newEnemy = new Enemy();
newEnemy.x = Math.random() * 2048;
newEnemy.y = -100;
game.addChild(newEnemy);
enemies.push(newEnemy);
}
// Handle game updates
game.update = function () {
// Update hero
hero.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (heroBullets[i].destroyed) {
heroBullets.splice(i, 1);
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (enemies[j].destroyed) {
enemies.splice(j, 1);
}
}
// Check for collisions
for (var k = heroBullets.length - 1; k >= 0; k--) {
for (var l = enemies.length - 1; l >= 0; l--) {
if (heroBullets[k].intersects(enemies[l])) {
heroBullets[k].destroy();
enemies[l].destroy();
heroBullets.splice(k, 1);
enemies.splice(l, 1);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Spawn new enemies
if (LK.ticks % 60 == 0) {
spawnEnemy();
}
};
// Handle touch events
game.down = function (x, y, obj) {
hero.attack();
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.up = function (x, y, obj) {
// No action needed on touch up
};
A single alien. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A hero space ship with cannon looking upwards. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A single bullet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.