/**** * 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 = -10; 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 = 2; self.update = function () { // Enemy movement logic goes here }; }); // The assets will be automatically created and loaded by the LK engine // Player class 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 () { // Player movement logic goes here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player, bullets and enemies var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 100; var bullets = []; var enemies = []; // Game update function game.update = function () { // Player movement logic game.move = function (x, y, obj) { if (x < player.x) { player.x -= player.speed; } else if (x > player.x) { player.x += player.speed; } }; // Shooting logic game.down = function (x, y, obj) { var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y; bullets.push(bullet); game.addChild(bullet); }; // Enemy spawning logic if (LK.ticks % 120 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = 0; enemies.push(enemy); game.addChild(enemy); } // Bullet and enemy update logic for (var i = bullets.length - 1; i >= 0; i--) { if (bullets[i].y < 0) { bullets[i].destroy(); bullets.splice(i, 1); } } for (var i = enemies.length - 1; i >= 0; i--) { if (enemies[i].y > 2732) { enemies[i].destroy(); enemies.splice(i, 1); } } // Collision detection for (var i = bullets.length - 1; i >= 0; i--) { for (var j = enemies.length - 1; j >= 0; j--) { if (bullets[i].intersects(enemies[j])) { bullets[i].destroy(); bullets.splice(i, 1); enemies[j].destroy(); enemies.splice(j, 1); break; } } } };
===================================================================
--- original.js
+++ change.js
@@ -65,15 +65,15 @@
player.x += player.speed;
}
};
// Shooting logic
- if (LK.keys['Space']) {
+ game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
- }
+ };
// Enemy spawning logic
if (LK.ticks % 120 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
Aliens. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Space craft facing upwards. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Power ups. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Rocks. 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.
Red ♥️. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.