/**** * Classes ****/ // Create a class for the bullets var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Bullet movement logic goes here }; }); // The assets will be automatically created and loaded by the LK engine. // Create a class for the spaceship var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Spaceship movement logic goes here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize the spaceship and bullets var spaceship = game.addChild(new Spaceship()); spaceship.x = 2048 / 2; spaceship.y = 2732 - 200; var bullets = []; // Handle spaceship movement game.move = function (x, y, obj) { spaceship.x = x; }; // Handle shooting game.down = function (x, y, obj) { var bullet = new Bullet(); bullet.x = spaceship.x; bullet.y = spaceship.y; bullets.push(bullet); game.addChild(bullet); }; // Update game state game.update = function () { for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].y -= 10; if (bullets[i].y < 0) { bullets[i].destroy(); bullets.splice(i, 1); } } };
/****
* Classes
****/
// Create a class for the bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Bullet movement logic goes here
};
});
// The assets will be automatically created and loaded by the LK engine.
// Create a class for the spaceship
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Spaceship movement logic goes here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize the spaceship and bullets
var spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
var bullets = [];
// Handle spaceship movement
game.move = function (x, y, obj) {
spaceship.x = x;
};
// Handle shooting
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = spaceship.x;
bullet.y = spaceship.y;
bullets.push(bullet);
game.addChild(bullet);
};
// Update game state
game.update = function () {
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].y -= 10;
if (bullets[i].y < 0) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
};