/**** * 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 self.y += self.speed; }; }); // 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 ****/ for (var i = 0; i < 5; i++) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * 2732; enemies.push(enemy); game.addChild(enemy); } var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; var bullets = []; var enemies = []; game.update = function () { // Game logic goes here // For example, shooting bullets, moving enemies, checking for collisions, etc. for (var i = bullets.length - 1; i >= 0; i--) { for (var j = enemies.length - 1; j >= 0; j--) { if (bullets[i].intersects(enemies[j])) { game.removeChild(bullets[i]); bullets.splice(i, 1); game.removeChild(enemies[j]); enemies.splice(j, 1); break; } } } }; game.down = function (x, y, obj) { // Player control logic goes here // For example, moving the player or shooting bullets var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y; bullets.push(bullet); game.addChild(bullet); }; game.move = function (x, y, obj) { // Player control logic goes here // For example, moving the player or shooting bullets player.x = x; player.y = y; }; game.up = function (x, y, obj) { // Player control logic goes here // For example, moving the player or shooting bullets };
===================================================================
--- original.js
+++ change.js
@@ -1,72 +1,98 @@
-/****
+/****
* 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;
- };
+ 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
- };
+ 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
+ self.y += self.speed;
+ };
});
// 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
- };
+ 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
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
+for (var i = 0; i < 5; i++) {
+ var enemy = new Enemy();
+ enemy.x = Math.random() * 2048;
+ enemy.y = Math.random() * 2732;
+ enemies.push(enemy);
+ game.addChild(enemy);
+}
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
var bullets = [];
var enemies = [];
game.update = function () {
- // Game logic goes here
- // For example, shooting bullets, moving enemies, checking for collisions, etc.
+ // Game logic goes here
+ // For example, shooting bullets, moving enemies, checking for collisions, etc.
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ for (var j = enemies.length - 1; j >= 0; j--) {
+ if (bullets[i].intersects(enemies[j])) {
+ game.removeChild(bullets[i]);
+ bullets.splice(i, 1);
+ game.removeChild(enemies[j]);
+ enemies.splice(j, 1);
+ break;
+ }
+ }
+ }
};
game.down = function (x, y, obj) {
- // Player control logic goes here
- // For example, moving the player or shooting bullets
+ // Player control logic goes here
+ // For example, moving the player or shooting bullets
+ var bullet = new Bullet();
+ bullet.x = player.x;
+ bullet.y = player.y;
+ bullets.push(bullet);
+ game.addChild(bullet);
};
game.move = function (x, y, obj) {
- // Player control logic goes here
- // For example, moving the player or shooting bullets
+ // Player control logic goes here
+ // For example, moving the player or shooting bullets
+ player.x = x;
+ player.y = y;
};
game.up = function (x, y, obj) {
- // Player control logic goes here
- // For example, moving the player or shooting bullets
+ // Player control logic goes here
+ // For example, moving the player or shooting bullets
};
\ No newline at end of file