===================================================================
--- original.js
+++ change.js
@@ -1,108 +1,146 @@
-/****
+/****
* Classes
****/
+var Ghost = Container.expand(function () {
+ var self = Container.call(this);
+ var ghostGraphics = self.attachAsset('ghost', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.5
+ });
+ self.speedX = Math.random() * 4 - 2;
+ self.speedY = Math.random() * 4 - 2;
+ self.move = function () {
+ self.x += self.speedX;
+ self.y += self.speedY;
+ // Reverse direction if hitting the edges of the screen
+ if (self.x < 50 || self.x > 1998) {
+ self.speedX *= -1;
+ }
+ if (self.y < 50 || self.y > 2682) {
+ self.speedY *= -1;
+ }
+ };
+});
// Block class
var Block = Container.expand(function () {
- var self = Container.call(this);
- var blockGraphics = self.attachAsset('block', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 5;
- self.move = function () {
- self.y += self.speed;
- };
+ var self = Container.call(this);
+ var blockGraphics = self.attachAsset('block', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 5;
+ self.move = function () {
+ self.y += self.speed;
+ };
});
// Character class
var Character = Container.expand(function () {
- var self = Container.call(this);
- var characterGraphics = self.attachAsset('character', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.moveLeft = function () {
- if (self.x > 50) {
- self.x -= 10;
- }
- };
- self.moveRight = function () {
- if (self.x < 1998) {
- self.x += 10;
- }
- };
+ var self = Container.call(this);
+ var characterGraphics = self.attachAsset('character', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.moveLeft = function () {
+ if (self.x > 50) {
+ self.x -= 10;
+ }
+ };
+ self.moveRight = function () {
+ if (self.x < 1998) {
+ self.x += 10;
+ }
+ };
});
-/****
+/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x1b1b1b // Init game with a dark grey background
});
-/****
+/****
* Game Code
****/
-// Initialize character
// Initialize assets used in this game.
+// Initialize character
var character = game.addChild(new Character());
character.x = 1024;
character.y = 2632;
// Initialize blocks array
var blocks = [];
// Initialize score
var score = 0;
var scoreTxt = new Text2(score.toString(), {
- size: 150,
- fill: "#ffffff"
+ size: 150,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle touch events
var touchPosition = null;
game.on('down', function (obj) {
- touchPosition = obj.event.getLocalPosition(game);
+ touchPosition = obj.event.getLocalPosition(game);
});
game.on('up', function (obj) {
- touchPosition = null;
+ touchPosition = null;
});
game.on('move', function (obj) {
- if (touchPosition) {
- touchPosition = obj.event.getLocalPosition(game);
- }
+ if (touchPosition) {
+ touchPosition = obj.event.getLocalPosition(game);
+ }
});
// Game tick event
LK.on('tick', function () {
- // Move character based on touch position
- if (touchPosition) {
- if (touchPosition.x < character.x) {
- character.moveLeft();
- } else if (touchPosition.x > character.x) {
- character.moveRight();
- }
- }
- // Move blocks
- for (var i = blocks.length - 1; i >= 0; i--) {
- blocks[i].move();
- // Check for collision with character
- if (blocks[i].intersects(character)) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- // Remove off-screen blocks
- if (blocks[i].y > 2782) {
- blocks[i].destroy();
- blocks.splice(i, 1);
- }
- }
- // Add new blocks
- if (LK.ticks % 60 == 0) {
- var newBlock = new Block();
- newBlock.x = Math.random() * 2048;
- newBlock.y = -50;
- blocks.push(newBlock);
- game.addChild(newBlock);
- }
- // Update score
- score++;
- scoreTxt.setText(score.toString());
+ // Move character based on touch position
+ if (touchPosition) {
+ if (touchPosition.x < character.x) {
+ character.moveLeft();
+ } else if (touchPosition.x > character.x) {
+ character.moveRight();
+ }
+ }
+ // Move blocks
+ for (var i = blocks.length - 1; i >= 0; i--) {
+ blocks[i].move();
+ // Check for collision with character
+ if (blocks[i].intersects(character)) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ // Remove off-screen blocks
+ if (blocks[i].y > 2782) {
+ blocks[i].destroy();
+ blocks.splice(i, 1);
+ }
+ }
+ // Move ghosts
+ for (var j = ghosts.length - 1; j >= 0; j--) {
+ ghosts[j].move();
+ // Game over if character touches a ghost
+ if (ghosts[j].intersects(character)) {
+ LK.effects.flashScreen(0x000000, 2000);
+ LK.showGameOver();
+ }
+ }
+ // Add new blocks
+ if (LK.ticks % 60 == 0) {
+ var newBlock = new Block();
+ newBlock.x = Math.random() * 2048;
+ newBlock.y = -50;
+ blocks.push(newBlock);
+ game.addChild(newBlock);
+ }
+ // Add new ghosts
+ if (LK.ticks % 180 == 0) {
+ var newGhost = new Ghost();
+ newGhost.x = Math.random() * 2048;
+ newGhost.y = Math.random() * 2732;
+ ghosts.push(newGhost);
+ game.addChild(newGhost);
+ }
+ // Update score
+ score++;
+ scoreTxt.setText(score.toString());
});
\ No newline at end of file
Ghost that is spooky. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Player that is scared. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
screaming ghost. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.