/**** * Classes ****/ // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Ninja class var Ninja = Container.expand(function () { var self = Container.call(this); var ninjaGraphics = self.attachAsset('ninja', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Ninja update logic }; self.move = function (x, y) { self.x = x; self.y = 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.update = function () { // Obstacle update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize variables var ninja; var enemies = []; var obstacles = []; var scoreTxt; var score = 0; // Initialize game elements function initGame() { ninja = game.addChild(new Ninja()); ninja.x = 2048 / 2; ninja.y = 2732 - 200; scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Spawn enemies and obstacles LK.setInterval(spawnEnemy, 1000); LK.setInterval(spawnObstacle, 2000); } // Spawn enemy function spawnEnemy() { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -100; enemies.push(enemy); game.addChild(enemy); } // Spawn obstacle function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = Math.random() * 2048; obstacle.y = Math.random() * 2732; obstacles.push(obstacle); game.addChild(obstacle); } // Handle game updates game.update = function () { // Update ninja ninja.update(); // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (ninja.intersects(enemies[i])) { gameOver(); } } // Update obstacles for (var j = obstacles.length - 1; j >= 0; j--) { obstacles[j].update(); if (ninja.intersects(obstacles[j])) { gameOver(); } } // Update score scoreTxt.setText(score); }; // Handle game over function gameOver() { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Handle touch/mouse events game.down = function (x, y, obj) { ninja.move(x, y); }; game.move = function (x, y, obj) { ninja.move(x, y); }; game.up = function (x, y, obj) { // No action needed on up event }; // Initialize game initGame();
/****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Ninja class
var Ninja = Container.expand(function () {
var self = Container.call(this);
var ninjaGraphics = self.attachAsset('ninja', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Ninja update logic
};
self.move = function (x, y) {
self.x = x;
self.y = 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.update = function () {
// Obstacle update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var ninja;
var enemies = [];
var obstacles = [];
var scoreTxt;
var score = 0;
// Initialize game elements
function initGame() {
ninja = game.addChild(new Ninja());
ninja.x = 2048 / 2;
ninja.y = 2732 - 200;
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Spawn enemies and obstacles
LK.setInterval(spawnEnemy, 1000);
LK.setInterval(spawnObstacle, 2000);
}
// Spawn enemy
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
enemies.push(enemy);
game.addChild(enemy);
}
// Spawn obstacle
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * 2732;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Handle game updates
game.update = function () {
// Update ninja
ninja.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (ninja.intersects(enemies[i])) {
gameOver();
}
}
// Update obstacles
for (var j = obstacles.length - 1; j >= 0; j--) {
obstacles[j].update();
if (ninja.intersects(obstacles[j])) {
gameOver();
}
}
// Update score
scoreTxt.setText(score);
};
// Handle game over
function gameOver() {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Handle touch/mouse events
game.down = function (x, y, obj) {
ninja.move(x, y);
};
game.move = function (x, y, obj) {
ninja.move(x, y);
};
game.up = function (x, y, obj) {
// No action needed on up event
};
// Initialize game
initGame();