/**** * Classes ****/ //<Assets used in the game will automatically appear here> // 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.update = 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.shoot = function () { var newBullet = new Bullet(); newBullet.x = self.x; newBullet.y = self.y; bullets.push(newBullet); game.addChild(newBullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize variables var bullets = []; var character = game.addChild(new Character()); character.x = 2048 / 2; character.y = 2732 - 200; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Handle game move events game.move = function (x, y, obj) { character.x = x; character.y = y; }; // Handle game down events game.down = function (x, y, obj) { character.shoot(); }; // Update game state game.update = function () { for (var i = bullets.length - 1; i >= 0; i--) { if (bullets[i].y < -50) { bullets[i].destroy(); bullets.splice(i, 1); } } if (LK.ticks % 30 == 0) { character.shoot(); } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// 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.update = 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.shoot = function () {
var newBullet = new Bullet();
newBullet.x = self.x;
newBullet.y = self.y;
bullets.push(newBullet);
game.addChild(newBullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var bullets = [];
var character = game.addChild(new Character());
character.x = 2048 / 2;
character.y = 2732 - 200;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle game move events
game.move = function (x, y, obj) {
character.x = x;
character.y = y;
};
// Handle game down events
game.down = function (x, y, obj) {
character.shoot();
};
// Update game state
game.update = function () {
for (var i = bullets.length - 1; i >= 0; i--) {
if (bullets[i].y < -50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
if (LK.ticks % 30 == 0) {
character.shoot();
}
};