/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.jumpStrength = -10;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
if (self.y > 2732 - ballGraphics.height / 2) {
self.y = 2732 - ballGraphics.height / 2;
self.speedY = 0;
}
};
self.jump = function () {
self.speedY = self.jumpStrength;
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
self.update = function () {
self.x += self.speedX;
if (self.x < -obstacleGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var ball;
var obstacles = [];
var score = 0;
var scoreTxt;
var obstacleInterval;
// Initialize game elements
function initGame() {
ball = game.addChild(new Ball());
ball.x = 2048 / 4;
ball.y = 2732 / 2;
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
obstacleInterval = LK.setInterval(createObstacle, 2000);
}
// Create obstacles
function createObstacle() {
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);
}
// Handle game over
function gameOver() {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Handle touch down event
game.down = function (x, y, obj) {
ball.jump();
};
// Update game state
game.update = function () {
ball.update();
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (ball.intersects(obstacles[i])) {
gameOver();
}
}
score += 1;
scoreTxt.setText(score);
};
// Initialize game
initGame(); /****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.jumpStrength = -10;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
if (self.y > 2732 - ballGraphics.height / 2) {
self.y = 2732 - ballGraphics.height / 2;
self.speedY = 0;
}
};
self.jump = function () {
self.speedY = self.jumpStrength;
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
self.update = function () {
self.x += self.speedX;
if (self.x < -obstacleGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var ball;
var obstacles = [];
var score = 0;
var scoreTxt;
var obstacleInterval;
// Initialize game elements
function initGame() {
ball = game.addChild(new Ball());
ball.x = 2048 / 4;
ball.y = 2732 / 2;
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
obstacleInterval = LK.setInterval(createObstacle, 2000);
}
// Create obstacles
function createObstacle() {
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);
}
// Handle game over
function gameOver() {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Handle touch down event
game.down = function (x, y, obj) {
ball.jump();
};
// Update game state
game.update = function () {
ball.update();
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (ball.intersects(obstacles[i])) {
gameOver();
}
}
score += 1;
scoreTxt.setText(score);
};
// Initialize game
initGame();