/****
* Classes
****/
// Assets will be automatically initialized based on usage in the code.
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = y;
};
self.shoot = function () {
// Shooting logic will be implemented here
};
});
// 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.move = function () {
self.y += self.speed;
};
});
// Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.move = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize important asset arrays
var obstacles = [];
var bullets = [];
var hero;
// Initialize hero
function initHero() {
hero = game.addChild(new Hero());
hero.x = game.width / 2;
hero.y = game.height - 200;
}
// Initialize game
function initGame() {
initHero();
}
// Game tick logic
function gameTick() {
// Move obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
// Check for collision with hero
if (obstacles[i].intersects(hero)) {
// End game logic
LK.showGameOver();
}
// Remove off-screen obstacles
if (obstacles[i].y > game.height + 50) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
// Move bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].move();
// Check for collision with obstacles
for (var k = obstacles.length - 1; k >= 0; k--) {
if (bullets[j].intersects(obstacles[k])) {
obstacles[k].destroy();
obstacles.splice(k, 1);
bullets[j].destroy();
bullets.splice(j, 1);
break;
}
}
// Remove off-screen bullets
if (bullets[j] && bullets[j].y < -50) {
bullets[j].destroy();
bullets.splice(j, 1);
}
}
}
// Touch event handlers
function handleTouchStart(obj) {
var touchPos = obj.event.getLocalPosition(game);
hero.move(touchPos.x, touchPos.y);
}
function handleTouchMove(obj) {
var touchPos = obj.event.getLocalPosition(game);
hero.move(touchPos.x, touchPos.y);
}
// Initialize game elements
initGame();
// Event listeners
game.on('down', handleTouchStart);
game.on('move', handleTouchMove);
// Game tick event
LK.on('tick', gameTick);