/****
* Classes
****/
// Asteroid class
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 7;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + asteroidGraphics.height) {
self.destroy();
asteroids.splice(asteroids.indexOf(self), 1);
}
};
});
// 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 + enemyGraphics.height) {
self.destroy();
enemies.splice(enemies.indexOf(self), 1);
}
};
});
//<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 () {
// Hero update logic
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// 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 < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
var StarBackground = Container.expand(function () {
var self = Container.call(this);
var background = self.attachAsset('starBackground', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize star background
var starBackground = new StarBackground();
starBackground.x = 2048 / 2;
starBackground.y = 0;
game.addChild(starBackground);
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemies = [];
var asteroids = [];
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);
// Handle game events
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.update = function () {
// Update star background
starBackground.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (hero.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update asteroids
for (var k = asteroids.length - 1; k >= 0; k--) {
asteroids[k].update();
if (hero.intersects(asteroids[k])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -enemy.height;
game.addChild(enemy);
enemies.push(enemy);
}
// Spawn asteroids
if (LK.ticks % 180 == 0) {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height;
game.addChild(asteroid);
asteroids.push(asteroid);
}
// Update score
scoreTxt.setText(score);
};
// Shoot bullets
game.down = function (x, y, obj) {
hero.shoot();
}; ===================================================================
--- original.js
+++ change.js
@@ -68,8 +68,22 @@
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
+var StarBackground = Container.expand(function () {
+ var self = Container.call(this);
+ var background = self.attachAsset('starBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 2;
+ self.update = function () {
+ self.y += self.speed;
+ if (self.y > 2732) {
+ self.y = 0;
+ }
+ };
+});
/****
* Initialize Game
****/
@@ -79,8 +93,13 @@
/****
* Game Code
****/
+// Initialize star background
+var starBackground = new StarBackground();
+starBackground.x = 2048 / 2;
+starBackground.y = 0;
+game.addChild(starBackground);
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemies = [];
@@ -106,8 +125,10 @@
hero.x = x;
hero.y = y;
};
game.update = function () {
+ // Update star background
+ starBackground.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
}