/**** * 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 = 3; self.update = function () { self.x += self.speed; }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); }); // Player class 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 () { if (self.movingUp) { self.y -= self.speed; } else { self.y += self.speed; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize assets used in this game. var player = game.addChild(new Player()); player.x = 1024; player.y = 2732; var enemy = game.addChild(new Enemy()); enemy.x = 0; enemy.y = Math.random() * 2732; var obstacles = []; for (var i = 0; i < 10; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = Math.random() * 2048; obstacle.y = Math.random() * 2732; obstacles.push(obstacle); } game.down = function (x, y, obj) { player.movingUp = true; }; game.up = function (x, y, obj) { player.movingUp = false; }; game.update = function () { if (player.intersects(enemy) || obstacles.some(function (obstacle) { return player.intersects(obstacle); })) { LK.showGameOver(); } };
===================================================================
--- original.js
+++ change.js
@@ -29,9 +29,13 @@
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
- self.y += self.speed;
+ if (self.movingUp) {
+ self.y -= self.speed;
+ } else {
+ self.y += self.speed;
+ }
};
});
/****
@@ -57,16 +61,15 @@
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * 2732;
obstacles.push(obstacle);
}
-game.move = function (x, y, obj) {
- player.x = x;
- player.y = y;
+game.down = function (x, y, obj) {
+ player.movingUp = true;
};
+game.up = function (x, y, obj) {
+ player.movingUp = false;
+};
game.update = function () {
- // Adjust the game view to follow the player
- game.x = -player.x + 1024;
- game.y = -player.y + 1366;
if (player.intersects(enemy) || obstacles.some(function (obstacle) {
return player.intersects(obstacle);
})) {
LK.showGameOver();