/**** * Classes ****/ // Cat class var Cat = Container.expand(function () { var self = Container.call(this); var catGraphics = self.attachAsset('cat', { anchorX: 0.5, anchorY: 0.5 }); self.jumpSpeed = -20; self.gravity = 1; self.velocityY = 0; self.update = function () { self.y += self.velocityY; self.velocityY += self.gravity; if (self.y > 2732 - catGraphics.height / 2) { self.y = 2732 - catGraphics.height / 2; self.velocityY = 0; } }; self.jump = function () { self.velocityY = self.jumpSpeed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Platform class var Platform = Container.expand(function () { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -platformGraphics.height; self.x = Math.random() * 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize game elements var platforms = []; var cat = game.addChild(new Cat()); cat.x = 1024; cat.y = 2000; // Create platforms for (var i = 0; i < 5; i++) { var platform = new Platform(); platform.x = Math.random() * 2048; platform.y = i * 500; platforms.push(platform); game.addChild(platform); } // Handle game updates var firstJump = true; game.update = function () { platforms.forEach(function (platform) { platform.update(); if (cat.intersects(platform) && cat.velocityY > 0 && cat.y < platform.y + platform.height / 2) { if (firstJump) { cat.jump(); firstJump = false; } } }); cat.update(); }; // Handle touch events game.down = function (x, y, obj) { cat.jump(); };
===================================================================
--- original.js
+++ change.js
@@ -65,13 +65,17 @@
platforms.push(platform);
game.addChild(platform);
}
// Handle game updates
+var firstJump = true;
game.update = function () {
platforms.forEach(function (platform) {
platform.update();
if (cat.intersects(platform) && cat.velocityY > 0 && cat.y < platform.y + platform.height / 2) {
- cat.jump();
+ if (firstJump) {
+ cat.jump();
+ firstJump = false;
+ }
}
});
cat.update();
};