/**** * 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 = -5; self.update = function () { self.y += self.speed; }; }); // The assets will be automatically created and loaded by the LK engine. // Car class var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.x += self.speed; if (self.x > 2048) { self.x = -carGraphics.width; } }; self.down = function (x, y, obj) { var bullet = game.addChild(new Bullet()); bullet.x = self.x; bullet.y = self.y; }; }); // 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 = -3; self.update = function () { self.y += self.speed; if (self.y < -obstacleGraphics.height) { self.y = 2732; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ var car = game.addChild(new Car()); car.x = 1024; car.y = 2732 - 200; var obstacles = []; for (var i = 0; i < 5; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = Math.random() * 2048; obstacle.y = Math.random() * 2732; obstacles.push(obstacle); } game.update = function () { for (var i = 0; i < obstacles.length; i++) { if (car.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Removed bullet firing from game update function }; game.move = function (x, y, obj) { car.x = x; car.y = y; };
===================================================================
--- original.js
+++ change.js
@@ -27,8 +27,13 @@
if (self.x > 2048) {
self.x = -carGraphics.width;
}
};
+ self.down = function (x, y, obj) {
+ var bullet = game.addChild(new Bullet());
+ bullet.x = self.x;
+ bullet.y = self.y;
+ };
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
@@ -71,14 +76,9 @@
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
- // Fire bullet every 30 ticks
- if (LK.ticks % 30 == 0) {
- var bullet = game.addChild(new Bullet());
- bullet.x = car.x;
- bullet.y = car.y;
- }
+ // Removed bullet firing from game update function
};
game.move = function (x, y, obj) {
car.x = x;
car.y = y;