/**** * 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; }; }); // 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) { self.y = 0; self.x = Math.random() * 2048; } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins 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 }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize hero var hero = game.addChild(new Hero()); hero.x = 1024; hero.y = 2500; // Initialize enemies var enemies = []; for (var i = 0; i < 5; i++) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * 1000; enemies.push(enemy); game.addChild(enemy); } // Initialize bullets var bullets = []; // Handle game move event game.move = function (x, y, obj) { hero.x = x; hero.y = y; }; // Handle game update event game.update = function () { // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; bullet.update(); if (bullet.y < 0) { bullet.destroy(); bullets.splice(i, 1); } } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { var enemy = enemies[j]; enemy.update(); if (hero.intersects(enemy)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Check for bullet collision for (var k = bullets.length - 1; k >= 0; k--) { var bullet = bullets[k]; if (bullet.intersects(enemy)) { // Destroy bullet and enemy bullet.destroy(); bullets.splice(k, 1); enemy.destroy(); enemies.splice(j, 1); break; } } } // Fire bullets if (LK.ticks % 20 == 0) { var newBullet = new Bullet(); newBullet.x = hero.x; newBullet.y = hero.y; bullets.push(newBullet); game.addChild(newBullet); } };
===================================================================
--- original.js
+++ change.js
@@ -1,105 +1,117 @@
-/****
+/****
* 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;
- };
+ 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;
+ };
});
// 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) {
- self.y = 0;
- self.x = Math.random() * 2048;
- }
- };
+ 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) {
+ self.y = 0;
+ self.x = Math.random() * 2048;
+ }
+ };
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins 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
- };
+ 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
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 1024;
hero.y = 2500;
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
- var enemy = new Enemy();
- enemy.x = Math.random() * 2048;
- enemy.y = Math.random() * 1000;
- enemies.push(enemy);
- game.addChild(enemy);
+ var enemy = new Enemy();
+ enemy.x = Math.random() * 2048;
+ enemy.y = Math.random() * 1000;
+ enemies.push(enemy);
+ game.addChild(enemy);
}
// Initialize bullets
var bullets = [];
// Handle game move event
game.move = function (x, y, obj) {
- hero.x = x;
- hero.y = y;
+ hero.x = x;
+ hero.y = y;
};
// Handle game update event
game.update = function () {
- // Update bullets
- for (var i = bullets.length - 1; i >= 0; i--) {
- var bullet = bullets[i];
- bullet.update();
- if (bullet.y < 0) {
- bullet.destroy();
- bullets.splice(i, 1);
- }
- }
- // Update enemies
- for (var j = 0; j < enemies.length; j++) {
- var enemy = enemies[j];
- enemy.update();
- if (hero.intersects(enemy)) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- }
- // Fire bullets
- if (LK.ticks % 20 == 0) {
- var newBullet = new Bullet();
- newBullet.x = hero.x;
- newBullet.y = hero.y;
- bullets.push(newBullet);
- game.addChild(newBullet);
- }
+ // Update bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ var bullet = bullets[i];
+ bullet.update();
+ if (bullet.y < 0) {
+ bullet.destroy();
+ bullets.splice(i, 1);
+ }
+ }
+ // Update enemies
+ for (var j = enemies.length - 1; j >= 0; j--) {
+ var enemy = enemies[j];
+ enemy.update();
+ if (hero.intersects(enemy)) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ // Check for bullet collision
+ for (var k = bullets.length - 1; k >= 0; k--) {
+ var bullet = bullets[k];
+ if (bullet.intersects(enemy)) {
+ // Destroy bullet and enemy
+ bullet.destroy();
+ bullets.splice(k, 1);
+ enemy.destroy();
+ enemies.splice(j, 1);
+ break;
+ }
+ }
+ }
+ // Fire bullets
+ if (LK.ticks % 20 == 0) {
+ var newBullet = new Bullet();
+ newBullet.x = hero.x;
+ newBullet.y = hero.y;
+ bullets.push(newBullet);
+ game.addChild(newBullet);
+ }
};
\ No newline at end of file