/**** * Classes ****/ // Bullet class var Bullet = Container.expand(function (x, y) { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.x = x; self.y = y; self.update = function () { self.y -= self.speed; if (self.y < 0) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('character', { anchorX: 0.5, anchorY: 0.5 }); self.health = 100; self.speed = 5; self.update = function () { // Update player logic }; self.shoot = function () { var bullet = new Bullet(self.x, self.y); bullets.push(bullet); game.addChild(bullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize players var player1 = new Player(); player1.x = 200; player1.y = 500; game.addChild(player1); var player2 = new Player(); player2.x = 1800; player2.y = 500; game.addChild(player2); // Initialize bullets array var bullets = []; // Game update loop game.update = function () { // Update players player1.update(); player2.update(); // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); if (bullets[i].intersects(player2)) { player2.health -= 10; bullets[i].destroy(); bullets.splice(i, 1); } } // Check for game over if (player1.health <= 0 || player2.health <= 0) { LK.showGameOver(); } }; // Player controls game.down = function (x, y, obj) { if (x < 1024) { player1.shoot(); } else { player2.shoot(); } };
/****
* Classes
****/
// Bullet class
var Bullet = Container.expand(function (x, y) {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.x = x;
self.y = y;
self.update = function () {
self.y -= self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.speed = 5;
self.update = function () {
// Update player logic
};
self.shoot = function () {
var bullet = new Bullet(self.x, self.y);
bullets.push(bullet);
game.addChild(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize players
var player1 = new Player();
player1.x = 200;
player1.y = 500;
game.addChild(player1);
var player2 = new Player();
player2.x = 1800;
player2.y = 500;
game.addChild(player2);
// Initialize bullets array
var bullets = [];
// Game update loop
game.update = function () {
// Update players
player1.update();
player2.update();
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].intersects(player2)) {
player2.health -= 10;
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Check for game over
if (player1.health <= 0 || player2.health <= 0) {
LK.showGameOver();
}
};
// Player controls
game.down = function (x, y, obj) {
if (x < 1024) {
player1.shoot();
} else {
player2.shoot();
}
};