/**** * Classes ****/ // Create a class for the obstacles 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 < -self.height) { self.destroy(); } }; }); // The assets will be automatically created and loaded by the LK engine // Create a class for the player character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Add code here to update the player's position based on input }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize player and add to game var player = game.addChild(new Player()); player.x = game.width / 2; player.y = game.height - player.height; // Initialize array to hold obstacles var obstacles = []; // Initialize score var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); LK.gui.top.addChild(scoreTxt); // Game update function game.update = function () { // Update player player.update(); // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { var obstacle = obstacles[i]; obstacle.update(); // Check for collision with player if (player.intersects(obstacle)) { LK.showGameOver(); } // Remove off-screen obstacles if (obstacle.y < -obstacle.height) { obstacles.splice(i, 1); obstacle.destroy(); // Increase score when an obstacle is successfully avoided score++; scoreTxt.setText(score); } } // Add new obstacles if (LK.ticks % 120 == 0) { // Every 2 seconds var obstacle = new Obstacle(); obstacle.x = Math.random() * (game.width - obstacle.width); obstacle.y = game.height; obstacles.push(obstacle); game.addChild(obstacle); } }; // Handle touch events game.down = function (x, y, obj) { player.x = x; player.y = y; }; game.move = function (x, y, obj) { player.x = x; player.y = y; };
/****
* Classes
****/
// Create a class for the obstacles
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 < -self.height) {
self.destroy();
}
};
});
// The assets will be automatically created and loaded by the LK engine
// Create a class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Add code here to update the player's position based on input
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize player and add to game
var player = game.addChild(new Player());
player.x = game.width / 2;
player.y = game.height - player.height;
// Initialize array to hold obstacles
var obstacles = [];
// Initialize score
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Game update function
game.update = function () {
// Update player
player.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
// Check for collision with player
if (player.intersects(obstacle)) {
LK.showGameOver();
}
// Remove off-screen obstacles
if (obstacle.y < -obstacle.height) {
obstacles.splice(i, 1);
obstacle.destroy();
// Increase score when an obstacle is successfully avoided
score++;
scoreTxt.setText(score);
}
}
// Add new obstacles
if (LK.ticks % 120 == 0) {
// Every 2 seconds
var obstacle = new Obstacle();
obstacle.x = Math.random() * (game.width - obstacle.width);
obstacle.y = game.height;
obstacles.push(obstacle);
game.addChild(obstacle);
}
};
// Handle touch events
game.down = function (x, y, obj) {
player.x = x;
player.y = y;
};
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};