/**** * Classes ****/ // Assets will be automatically created and loaded during gameplay // Bird class var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.gravity = 0.5; self.lift = -10; self.velocity = 0; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; if (self.y > 2732 - birdGraphics.height / 2 || self.y < birdGraphics.height / 2) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }; self.flap = function () { self.velocity = self.lift; }; }); // Coin class var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -coinGraphics.width / 2) { self.destroy(); } }; }); // Downside Obstacle class var DownsideObstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -obstacleGraphics.width / 2) { self.destroy(); } }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -obstacleGraphics.width / 2) { self.destroy(); } }; }); // Rod class var Rod = Container.expand(function () { var self = Container.call(this); var rodGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -rodGraphics.width / 2) { self.destroy(); } }; }); // Upside Obstacle class var UpsideObstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -obstacleGraphics.width / 2) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ var bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; var obstacles = []; var coins = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function spawnCoin() { var coin = new Coin(); coin.x = 2048 + coin.width / 2; coin.y = Math.random() * (2732 - coin.height) + coin.height / 2; coins.push(coin); game.addChild(coin); } game.down = function (x, y, obj) { bird.flap(); }; game.update = function () { if (LK.ticks % 150 == 0) { var rod = new Rod(); rod.x = 2048 + rod.width / 2; rod.y = 2732; game.addChild(rod); } bird.update(); if (bird.y > 2732 - bird.height / 2 || bird.y < bird.height / 2) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (bird.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } for (var j = coins.length - 1; j >= 0; j--) { coins[j].update(); if (bird.intersects(coins[j])) { score += 1; scoreTxt.setText(score); coins[j].destroy(); coins.splice(j, 1); } } if (LK.ticks % 120 == 0) { var upsideObstacle = new UpsideObstacle(); upsideObstacle.x = 2048 + upsideObstacle.width / 2; upsideObstacle.y = Math.random() * (2732 / 2 - upsideObstacle.height / 2); obstacles.push(upsideObstacle); game.addChild(upsideObstacle); var downsideObstacle = new DownsideObstacle(); downsideObstacle.x = 2048 + downsideObstacle.width / 2; downsideObstacle.y = Math.random() * (2732 / 2 - downsideObstacle.height / 2) + 2732 / 2; obstacles.push(downsideObstacle); game.addChild(downsideObstacle); } if (LK.ticks % 180 == 0) { spawnCoin(); } };
===================================================================
--- original.js
+++ change.js
@@ -121,15 +121,8 @@
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
-function spawnObstacle() {
- var obstacle = new Obstacle();
- obstacle.x = 2048 + obstacle.width / 2;
- obstacle.y = Math.random() * (2732 - obstacle.height) + obstacle.height / 2;
- obstacles.push(obstacle);
- game.addChild(obstacle);
-}
function spawnCoin() {
var coin = new Coin();
coin.x = 2048 + coin.width / 2;
coin.y = Math.random() * (2732 - coin.height) + coin.height / 2;
@@ -167,9 +160,8 @@
coins.splice(j, 1);
}
}
if (LK.ticks % 120 == 0) {
- spawnObstacle();
var upsideObstacle = new UpsideObstacle();
upsideObstacle.x = 2048 + upsideObstacle.width / 2;
upsideObstacle.y = Math.random() * (2732 / 2 - upsideObstacle.height / 2);
obstacles.push(upsideObstacle);