/****
* Classes
****/
// AlienShip class
var AlienShip = Container.expand(function () {
var self = Container.call(this);
var alienShipGraphics = self.attachAsset('alienShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Bullet class
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();
}
};
});
// Meteor class
var Meteor = Container.expand(function () {
var self = Container.call(this);
var meteorGraphics = self.attachAsset('meteor', {
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>
// Spaceship class
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Spaceship update logic
};
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var spaceship;
var meteors = [];
var alienShips = [];
var bullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create spaceship
spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
// Handle touch events
game.down = function (x, y, obj) {
spaceship.move(x, y);
};
game.move = function (x, y, obj) {
spaceship.move(x, y);
};
// Update game state
game.update = function () {
// Update spaceship
spaceship.update();
// Update meteors
for (var i = meteors.length - 1; i >= 0; i--) {
meteors[i].update();
if (meteors[i].intersects(spaceship)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update alien ships
for (var i = alienShips.length - 1; i >= 0; i--) {
alienShips[i].update();
if (alienShips[i].intersects(spaceship)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
for (var j = meteors.length - 1; j >= 0; j--) {
if (bullets[i].intersects(meteors[j])) {
meteors[j].destroy();
bullets[i].destroy();
meteors.splice(j, 1);
bullets.splice(i, 1);
score++;
scoreTxt.setText(score);
break;
}
}
for (var k = alienShips.length - 1; k >= 0; k--) {
if (bullets[i].intersects(alienShips[k])) {
alienShips[k].destroy();
bullets[i].destroy();
alienShips.splice(k, 1);
bullets.splice(i, 1);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Spawn meteors
if (LK.ticks % 60 == 0) {
var newMeteor = new Meteor();
newMeteor.x = Math.random() * 2048;
newMeteor.y = -50;
meteors.push(newMeteor);
game.addChild(newMeteor);
}
// Spawn alien ships
if (LK.ticks % 120 == 0) {
var newAlienShip = new AlienShip();
newAlienShip.x = Math.random() * 2048;
newAlienShip.y = -50;
alienShips.push(newAlienShip);
game.addChild(newAlienShip);
}
// Fire bullets
if (LK.ticks % 15 == 0) {
var newBullet = new Bullet();
newBullet.x = spaceship.x;
newBullet.y = spaceship.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
};