/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
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 = -20;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Assets will be automatically created and loaded during gameplay
// Hero class
var JetPilot = Container.expand(function () {
var self = Container.call(this);
var jetPilotGraphics = self.attachAsset('jetPilot', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// JetPilot 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: 0x1a1a1a // Init game with dark gray background
});
/****
* Game Code
****/
// Initialize variables
var hero;
var zombies = [];
var bullets = [];
var asteroids = [];
var score = 0;
var scoreTxt;
var coins = [];
// Initialize jet pilot
jetPilot = game.addChild(new JetPilot());
jetPilot.x = 2048 / 2;
jetPilot.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) {
jetPilot.x = x;
jetPilot.y = y;
};
// Handle game down events
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = jetPilot.x;
bullet.y = jetPilot.y;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
};
// Update game logic
game.update = function () {
// Update jet pilot
jetPilot.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 asteroids
for (var j = asteroids.length - 1; j >= 0; j--) {
asteroids[j].update();
if (asteroids[j].intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update coins
for (var i = coins.length - 1; i >= 0; i--) {
coins[i].update();
if (coins[i].intersects(hero)) {
coins[i].destroy();
coins.splice(i, 1);
score++;
scoreTxt.setText(score);
}
}
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);
}
// Spawn new asteroids
if (LK.ticks % 120 == 0) {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -150;
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn new coins
if (LK.ticks % 90 == 0) {
var coin = new Coin();
coin.x = Math.random() * 2048;
coin.y = -50;
coins.push(coin);
game.addChild(coin);
}
}; ===================================================================
--- original.js
+++ change.js
@@ -45,17 +45,17 @@
};
});
// Assets will be automatically created and loaded during gameplay
// Hero class
-var Hero = Container.expand(function () {
+var JetPilot = Container.expand(function () {
var self = Container.call(this);
- var heroGraphics = self.attachAsset('hero', {
+ var jetPilotGraphics = self.attachAsset('jetPilot', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
- // Hero update logic
+ // JetPilot update logic
};
});
// Zombie class
var Zombie = Container.expand(function () {
@@ -90,12 +90,12 @@
var asteroids = [];
var score = 0;
var scoreTxt;
var coins = [];
-// Initialize hero
-hero = game.addChild(new Hero());
-hero.x = 2048 / 2;
-hero.y = 2732 - 200;
+// Initialize jet pilot
+jetPilot = game.addChild(new JetPilot());
+jetPilot.x = 2048 / 2;
+jetPilot.y = 2732 - 200;
// Initialize score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
@@ -103,24 +103,24 @@
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;
+ jetPilot.x = x;
+ jetPilot.y = y;
};
// Handle game down events
game.down = function (x, y, obj) {
var bullet = new Bullet();
- bullet.x = hero.x;
- bullet.y = hero.y;
+ bullet.x = jetPilot.x;
+ bullet.y = jetPilot.y;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
};
// Update game logic
game.update = function () {
- // Update hero
- hero.update();
+ // Update jet pilot
+ jetPilot.update();
// Update zombies
for (var i = zombies.length - 1; i >= 0; i--) {
zombies[i].update();
if (zombies[i].intersects(hero)) {