/**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Coin update logic if needed }; }); // 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 coins = []; 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(); } } } // Create coins if (LK.ticks % 120 == 0) { var coin = new Coin(); coin.x = Math.random() * 2048; coin.y = Math.random() * 2732; game.addChild(coin); coins.push(coin); } // 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 += 5; } } // 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(); // Check for coin collection for (var i = coins.length - 1; i >= 0; i--) { if (coins[i].intersects(hero)) { coins[i].destroy(); coins.splice(i, 1); score += 5; } } }; 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
@@ -1,7 +1,17 @@
/****
* Classes
****/
+var Coin = Container.expand(function () {
+ var self = Container.call(this);
+ var coinGraphics = self.attachAsset('coin', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Coin update logic if needed
+ };
+});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemyHD', {
@@ -85,8 +95,9 @@
var hero;
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
+var coins = [];
var score = 0;
// Create hero
hero = new Hero();
hero.x = 2048 / 2;
@@ -136,8 +147,25 @@
LK.showGameOver();
}
}
}
+ // Create coins
+ if (LK.ticks % 120 == 0) {
+ var coin = new Coin();
+ coin.x = Math.random() * 2048;
+ coin.y = Math.random() * 2732;
+ game.addChild(coin);
+ coins.push(coin);
+ }
+ // 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 += 5;
+ }
+ }
// Enemies shoot
if (LK.ticks % 60 == 0) {
for (var i = 0; i < enemies.length; i++) {
enemies[i].shoot();
@@ -146,8 +174,16 @@
};
// Handle touch events
game.down = function (x, y, obj) {
hero.shoot();
+ // Check for coin collection
+ for (var i = coins.length - 1; i >= 0; i--) {
+ if (coins[i].intersects(hero)) {
+ coins[i].destroy();
+ coins.splice(i, 1);
+ score += 5;
+ }
+ }
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;