/**** * Classes ****/ // Class for Bullet var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y -= self.speed; }; }); // Class for Enemy var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { // Update enemy position or state }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for Player var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Update player position or state }; self.down = function (x, y, obj) { // Handle player interaction }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize background var background = game.addChild(LK.getAsset('Background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 150; // 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 updates game.update = function () { // Update player player.update(); // Update enemies for (var i = 0; i < enemies.length; i++) { enemies[i].update(); } // Update bullets for (var j = bullets.length - 1; j >= 0; j--) { bullets[j].update(); if (bullets[j].y < 0) { bullets[j].destroy(); bullets.splice(j, 1); } } // Check for collisions for (var k = enemies.length - 1; k >= 0; k--) { if (player.intersects(enemies[k])) { // Handle player and enemy collision LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } for (var l = bullets.length - 1; l >= 0; l--) { if (bullets[l].intersects(enemies[k])) { // Handle bullet and enemy collision enemies[k].destroy(); enemies.splice(k, 1); bullets[l].destroy(); bullets.splice(l, 1); break; } } } }; // Handle player shooting game.down = function (x, y, obj) { var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y; bullets.push(bullet); game.addChild(bullet); }; // Handle player movement game.move = function (x, y, obj) { player.x = x; player.y = y; };
===================================================================
--- original.js
+++ change.js
@@ -97,8 +97,18 @@
// Handle player and enemy collision
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
+ for (var l = bullets.length - 1; l >= 0; l--) {
+ if (bullets[l].intersects(enemies[k])) {
+ // Handle bullet and enemy collision
+ enemies[k].destroy();
+ enemies.splice(k, 1);
+ bullets[l].destroy();
+ bullets.splice(l, 1);
+ break;
+ }
+ }
}
};
// Handle player shooting
game.down = function (x, y, obj) {
Player man. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Bullet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Enemy. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Battle ground. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.