/****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemyHD', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 500;
self.update = function () {
// Update logic for enemy
};
self.shoot = function () {
var bullet = new EnemyBullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
enemyBullets.push(bullet);
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('enemyBulletHD', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<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('heroHD', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 400;
self.update = function () {
// Update logic for hero
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBulletHD', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
var score = 0;
// Create hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Create enemies
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = (i + 1) * 300;
enemy.y = 200;
game.addChild(enemy);
enemies.push(enemy);
}
// Update function
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].health <= 0) {
enemies[i].destroy();
enemies.splice(i, 1);
score += 10;
}
}
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].health -= 50;
heroBullets[i].destroy();
heroBullets.splice(i, 1);
break;
}
}
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].update();
if (enemyBullets[i].intersects(hero)) {
hero.health -= 10;
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
if (hero.health <= 0) {
LK.showGameOver();
}
}
}
// Enemies shoot
if (LK.ticks % 60 == 0) {
for (var i = 0; i < enemies.length; i++) {
enemies[i].shoot();
}
}
};
// Handle touch events
game.down = function (x, y, obj) {
hero.shoot();
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.up = function (x, y, obj) {
// No action needed on touch up
}; ===================================================================
--- original.js
+++ change.js
@@ -7,9 +7,9 @@
var enemyGraphics = self.attachAsset('enemyHD', {
anchorX: 0.5,
anchorY: 0.5
});
- self.health = 100;
+ self.health = 500;
self.update = function () {
// Update logic for enemy
};
self.shoot = function () {
@@ -42,9 +42,9 @@
var heroGraphics = self.attachAsset('heroHD', {
anchorX: 0.5,
anchorY: 0.5
});
- self.health = 500;
+ self.health = 400;
self.update = function () {
// Update logic for hero
};
self.shoot = function () {