/**** * Classes ****/ // The assets will be automatically created and loaded by the LK engine. // Create a Bird class var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0; self.gravity = 0.5; self.lift = -15; self.update = function () { self.speed += self.gravity; self.y += self.speed; if (self.y > 2732) { self.y = 2732; self.speed = 0; } if (self.y < 0) { self.y = 0; self.speed = 0; } }; self.up = function () { self.speed += self.lift; }; }); // Create a Bridge class var Bridge = Container.expand(function () { var self = Container.call(this); var bridgeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.x -= self.speed; }; }); // Create a Pipe class var Pipe = Container.expand(function () { var self = Container.call(this); var pipeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.x -= self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Sky blue background }); /**** * Game Code ****/ var bird = game.addChild(new Bird()); var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); bird.x = 512; bird.y = 1366; var obstacles = []; game.update = function () { if (LK.ticks % 120 == 0) { var bridge = new Bridge(); bridge.x = 2048; bridge.y = Math.random() * (2048 - 512) + 512; obstacles.push(bridge); game.addChild(bridge); } for (var i = obstacles.length - 1; i >= 0; i--) { if (obstacles[i].x < -obstacles[i].width) { obstacles[i].destroy(); obstacles.splice(i, 1); score += 1; scoreTxt.setText(score); } if (bird.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } }; game.down = function (x, y, obj) { bird.up(); };
===================================================================
--- original.js
+++ change.js
@@ -72,30 +72,25 @@
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
bird.x = 512;
bird.y = 1366;
-var pipes = [];
+var obstacles = [];
game.update = function () {
if (LK.ticks % 120 == 0) {
- var pipe = new Pipe();
- pipe.x = 2048;
- pipe.y = Math.random() * (2048 - 512) + 512;
- pipes.push(pipe);
- game.addChild(pipe);
var bridge = new Bridge();
bridge.x = 2048;
bridge.y = Math.random() * (2048 - 512) + 512;
- pipes.push(bridge);
+ obstacles.push(bridge);
game.addChild(bridge);
}
- for (var i = pipes.length - 1; i >= 0; i--) {
- if (pipes[i].x < -pipes[i].width) {
- pipes[i].destroy();
- pipes.splice(i, 1);
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ if (obstacles[i].x < -obstacles[i].width) {
+ obstacles[i].destroy();
+ obstacles.splice(i, 1);
score += 1;
scoreTxt.setText(score);
}
- if (bird.intersects(pipes[i])) {
+ if (bird.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}