/**** * Classes ****/ var Block = Container.expand(function () { var self = Container.call(this); var blockGraphics = self.attachAsset('block', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 4; self.falling = false; self.move = function (hero) { if (!self.falling) { if (self.y < 2732 - self.height) { self.y += self.speed; self.rotation += 0.1; if (self.intersects(hero)) { hero.balance(self); } } else { self.falling = true; } } else { self.y += self.speed; if (self.y > 2732) { self.destroy(); } } }; }); var Pizza = Container.expand(function () { var self = Container.call(this); var pizzaGraphics = self.attachAsset('pizza', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; }); var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); heroGraphics.scale.set(4); self.speed = 5; self.move = function (pos) { if (pos) { if (pos) { self.x = pos.x; } } }; self.balance = function (block) { block.destroy(); self.parent.score += 1; var scoreTxt = LK.gui.topCenter.children[0]; scoreTxt.setText(self.parent.score.toString()); }; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var background = game.attachAsset('background', {}); background.width = 2048; background.height = 2732; var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - hero.height; var blocks = []; game.score = 0; game.gameOver = false; var scoreTxt = new Text2(game.score.toString(), { size: 150, fill: '#ffffff', name: 'scoreTxt' }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var spawnBlocks = function spawnBlocks() { var block = new Block(); block.x = Math.random() * 2048; block.y = 0; game.addChild(block); blocks.push(block); }; LK.setInterval(spawnBlocks, 1000); game.on('move', function (obj) { var event = obj.event; var pos = event.getLocalPosition(game); hero.move(pos); }); LK.on('tick', function () { if (game.gameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } for (var a = blocks.length - 1; a >= 0; a--) { blocks[a].move(hero); if (blocks[a].y < -50) { blocks[a].destroy(); blocks.splice(a, 1); } } });
/****
* Classes
****/
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.falling = false;
self.move = function (hero) {
if (!self.falling) {
if (self.y < 2732 - self.height) {
self.y += self.speed;
self.rotation += 0.1;
if (self.intersects(hero)) {
hero.balance(self);
}
} else {
self.falling = true;
}
} else {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
}
};
});
var Pizza = Container.expand(function () {
var self = Container.call(this);
var pizzaGraphics = self.attachAsset('pizza', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
heroGraphics.scale.set(4);
self.speed = 5;
self.move = function (pos) {
if (pos) {
if (pos) {
self.x = pos.x;
}
}
};
self.balance = function (block) {
block.destroy();
self.parent.score += 1;
var scoreTxt = LK.gui.topCenter.children[0];
scoreTxt.setText(self.parent.score.toString());
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var background = game.attachAsset('background', {});
background.width = 2048;
background.height = 2732;
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - hero.height;
var blocks = [];
game.score = 0;
game.gameOver = false;
var scoreTxt = new Text2(game.score.toString(), {
size: 150,
fill: '#ffffff',
name: 'scoreTxt'
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var spawnBlocks = function spawnBlocks() {
var block = new Block();
block.x = Math.random() * 2048;
block.y = 0;
game.addChild(block);
blocks.push(block);
};
LK.setInterval(spawnBlocks, 1000);
game.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
hero.move(pos);
});
LK.on('tick', function () {
if (game.gameOver) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
for (var a = blocks.length - 1; a >= 0; a--) {
blocks[a].move(hero);
if (blocks[a].y < -50) {
blocks[a].destroy();
blocks.splice(a, 1);
}
}
});