/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.speedX = 0;
self.jumpStrength = -10;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
self.x += self.speedX;
if (self.x < birdGraphics.width / 2) {
self.x = birdGraphics.width / 2;
self.speedX = 0;
}
if (self.x > 2048 - birdGraphics.width / 2) {
self.x = 2048 - birdGraphics.width / 2;
self.speedX = 0;
}
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.speedY = 0;
}
};
self.jump = function () {
self.speedY = self.jumpStrength;
};
});
// Platform class
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5;
if (self.y > 2732 + platformGraphics.height / 2) {
self.y = -platformGraphics.height / 2;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Init game with sky blue background
});
/****
* Game Code
****/
// Initialize bird
var bird = game.addChild(new Bird());
bird.x = 2048 / 2;
bird.y = 2732 / 2;
bird.speedX = 0;
// Initialize platforms
var 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 touch events
game.down = function (x, y, obj) {
bird.jump();
if (x < 2048 / 2) {
bird.speedX = -5;
} else {
bird.speedX = 5;
}
};
// Update game logic
game.update = function () {
bird.update();
for (var i = 0; i < platforms.length; i++) {
platforms[i].update();
if (bird.intersects(platforms[i]) && bird.speedY > 0) {
bird.jump();
bird.speedX = 0;
}
}
};