/**** * Classes ****/ // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -100; self.x = Math.random() * 2048; } }; }); // PowerUp class var PowerUp = Container.expand(function () { var self = Container.call(this); var powerUpGraphics = self.attachAsset('powerUp', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { self.y += 3; if (self.y > 2732) { self.y = -100; self.x = Math.random() * 2048; } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins 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 game elements var spaceship = game.addChild(new Spaceship()); spaceship.x = 1024; spaceship.y = 2400; var obstacles = []; for (var i = 0; i < 5; i++) { var obstacle = new Obstacle(); obstacle.x = Math.random() * 2048; obstacle.y = Math.random() * -2732; obstacles.push(obstacle); game.addChild(obstacle); } var powerUps = []; for (var i = 0; i < 3; i++) { var powerUp = new PowerUp(); powerUp.x = Math.random() * 2048; powerUp.y = Math.random() * -2732; powerUps.push(powerUp); game.addChild(powerUp); } // Handle game events game.down = function (x, y, obj) { spaceship.move(x, y); }; game.update = function () { for (var i = 0; i < obstacles.length; i++) { obstacles[i].update(); if (spaceship.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } for (var i = 0; i < powerUps.length; i++) { powerUps[i].update(); if (spaceship.intersects(powerUps[i])) { LK.setScore(LK.getScore() + 10); powerUps[i].destroy(); powerUps.splice(i, 1); } } };
/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -100;
self.x = Math.random() * 2048;
}
};
});
// PowerUp class
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 3;
if (self.y > 2732) {
self.y = -100;
self.x = Math.random() * 2048;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins 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 game elements
var spaceship = game.addChild(new Spaceship());
spaceship.x = 1024;
spaceship.y = 2400;
var obstacles = [];
for (var i = 0; i < 5; i++) {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * -2732;
obstacles.push(obstacle);
game.addChild(obstacle);
}
var powerUps = [];
for (var i = 0; i < 3; i++) {
var powerUp = new PowerUp();
powerUp.x = Math.random() * 2048;
powerUp.y = Math.random() * -2732;
powerUps.push(powerUp);
game.addChild(powerUp);
}
// Handle game events
game.down = function (x, y, obj) {
spaceship.move(x, y);
};
game.update = function () {
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
if (spaceship.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
for (var i = 0; i < powerUps.length; i++) {
powerUps[i].update();
if (spaceship.intersects(powerUps[i])) {
LK.setScore(LK.getScore() + 10);
powerUps[i].destroy();
powerUps.splice(i, 1);
}
}
};