/****
* Classes
****/
// 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 = -20;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Assets will be automatically created and loaded during gameplay
// 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
};
});
// Zombie class
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.attachAsset('zombie', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var hero;
var zombies = [];
var bullets = [];
var score = 0;
var scoreTxt;
// Initialize hero
hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle game move events
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
// Handle game down events
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = hero.x;
bullet.y = hero.y;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
};
// Update game logic
game.update = function () {
// Update hero
hero.update();
// Update zombies
for (var i = zombies.length - 1; i >= 0; i--) {
zombies[i].update();
if (zombies[i].intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
for (var k = zombies.length - 1; k >= 0; k--) {
if (bullets[j].intersects(zombies[k])) {
bullets[j].destroy();
bullets.splice(j, 1);
zombies[k].destroy();
zombies.splice(k, 1);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Spawn new zombies
if (LK.ticks % 60 == 0) {
var zombie = new Zombie();
zombie.x = Math.random() * 2048;
zombie.y = -100;
zombies.push(zombie);
game.addChild(zombie);
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,61 +1,61 @@
-/****
+/****
* Classes
-****/
+****/
// 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 = -15;
- self.update = function () {
- self.y += self.speed;
- if (self.y < 0) {
- self.destroy();
- }
- };
+ var self = Container.call(this);
+ var bulletGraphics = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -20;
+ self.update = function () {
+ self.y += self.speed;
+ if (self.y < 0) {
+ self.destroy();
+ }
+ };
});
// Assets will be automatically created and loaded during gameplay
// 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
- };
+ 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
+ };
});
// Zombie class
var Zombie = Container.expand(function () {
- var self = Container.call(this);
- var zombieGraphics = self.attachAsset('zombie', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 2;
- self.update = function () {
- self.y += self.speed;
- if (self.y > 2732) {
- self.destroy();
- }
- };
+ var self = Container.call(this);
+ var zombieGraphics = self.attachAsset('zombie', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 2;
+ self.update = function () {
+ self.y += self.speed;
+ if (self.y > 2732) {
+ self.destroy();
+ }
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Initialize variables
var hero;
var zombies = [];
var bullets = [];
@@ -66,58 +66,59 @@
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize score text
scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+ size: 150,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle game move events
game.move = function (x, y, obj) {
- hero.x = x;
- hero.y = y;
+ hero.x = x;
+ hero.y = y;
};
// Handle game down events
game.down = function (x, y, obj) {
- var bullet = new Bullet();
- bullet.x = hero.x;
- bullet.y = hero.y;
- bullets.push(bullet);
- game.addChild(bullet);
+ var bullet = new Bullet();
+ bullet.x = hero.x;
+ bullet.y = hero.y;
+ bullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
};
// Update game logic
game.update = function () {
- // Update hero
- hero.update();
- // Update zombies
- for (var i = zombies.length - 1; i >= 0; i--) {
- zombies[i].update();
- if (zombies[i].intersects(hero)) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- }
- // Update bullets
- for (var j = bullets.length - 1; j >= 0; j--) {
- bullets[j].update();
- for (var k = zombies.length - 1; k >= 0; k--) {
- if (bullets[j].intersects(zombies[k])) {
- bullets[j].destroy();
- bullets.splice(j, 1);
- zombies[k].destroy();
- zombies.splice(k, 1);
- score++;
- scoreTxt.setText(score);
- break;
- }
- }
- }
- // Spawn new zombies
- if (LK.ticks % 60 == 0) {
- var zombie = new Zombie();
- zombie.x = Math.random() * 2048;
- zombie.y = -100;
- zombies.push(zombie);
- game.addChild(zombie);
- }
+ // Update hero
+ hero.update();
+ // Update zombies
+ for (var i = zombies.length - 1; i >= 0; i--) {
+ zombies[i].update();
+ if (zombies[i].intersects(hero)) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ }
+ // Update bullets
+ for (var j = bullets.length - 1; j >= 0; j--) {
+ bullets[j].update();
+ for (var k = zombies.length - 1; k >= 0; k--) {
+ if (bullets[j].intersects(zombies[k])) {
+ bullets[j].destroy();
+ bullets.splice(j, 1);
+ zombies[k].destroy();
+ zombies.splice(k, 1);
+ score++;
+ scoreTxt.setText(score);
+ break;
+ }
+ }
+ }
+ // Spawn new zombies
+ if (LK.ticks % 60 == 0) {
+ var zombie = new Zombie();
+ zombie.x = Math.random() * 2048;
+ zombie.y = -100;
+ zombies.push(zombie);
+ game.addChild(zombie);
+ }
};
\ No newline at end of file