/**** * Classes ****/ // Define the Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Define the Player class //<Assets used in the game will automatically appear here> // Initialize player var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Player update logic }; self.move = function (x, y) { self.x = x; self.y = y; }; }); /**** * Initialize Game ****/ //<Assets used in the game will automatically appear here> // Define the Player class var game = new LK.Game({ backgroundColor: 0x000000 //Init game with blue background }); /**** * Game Code ****/ var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 200; // Initialize obstacles array var obstacles = []; // Function to handle player movement function handleMove(x, y, obj) { player.move(x, y); } // Function to spawn obstacles function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = Math.random() * 2048; obstacle.y = -50; obstacles.push(obstacle); game.addChild(obstacle); } // Set interval to spawn obstacles var obstacleInterval = LK.setInterval(spawnObstacle, 1000); // Game update function game.update = function () { for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (player.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } }; // Event listeners for player movement game.move = handleMove; game.down = handleMove; game.up = function (x, y, obj) { // Stop player movement on touch release }; // Center the player on screen player.x = 2048 / 2; player.y = 2732 - 200;
===================================================================
--- original.js
+++ change.js
@@ -15,10 +15,11 @@
self.destroy();
}
};
});
-//<Assets used in the game will automatically appear here>
// Define the Player class
+//<Assets used in the game will automatically appear here>
+// Initialize player
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
@@ -36,16 +37,17 @@
/****
* Initialize Game
****/
+//<Assets used in the game will automatically appear here>
+// Define the Player class
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with blue background
});
/****
* Game Code
****/
-// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize obstacles array