/**** * Classes ****/ // Define the 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 = 15; self.update = function () { self.y -= self.speed; }; }); // Define the Car class var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Update logic for car }; }); // Define the Gun class var Gun = Container.expand(function () { var self = Container.call(this); var gunGraphics = self.attachAsset('gun', { anchorX: 0.5, anchorY: 0.5 }); self.shoot = function () { // Shooting logic }; self.update = function () { // Update logic for gun }; }); //<Assets used in the game will automatically appear here> // Define the Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Update logic for hero }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var hero; var guns = []; var cars = []; var bullets = []; var scoreTxt; // Initialize the game elements function initGame() { // Create hero hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 / 2; // Create guns for (var i = 0; i < 10; i++) { var gun = new Gun(); gun.x = Math.random() * 2048; gun.y = Math.random() * 2732; guns.push(gun); game.addChild(gun); } // Create cars for (var i = 0; i < 20; i++) { var car = new Car(); car.x = Math.random() * 2048; car.y = Math.random() * 2732; cars.push(car); game.addChild(car); } // Create score text scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); } // Handle game updates game.update = function () { // Update hero hero.update(); // Update guns for (var i = 0; i < guns.length; i++) { guns[i].update(); } // Update cars for (var i = 0; i < cars.length; i++) { cars[i].update(); } // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); if (bullets[i].y < -50) { bullets[i].destroy(); bullets.splice(i, 1); } } // Fire bullet if (LK.ticks % 30 == 0) { var newBullet = new Bullet(); newBullet.x = hero.x; newBullet.y = hero.y; bullets.push(newBullet); game.addChild(newBullet); } }; // Handle touch events game.down = function (x, y, obj) { hero.x = x; hero.y = y; }; // Initialize the game initGame();
/****
* Classes
****/
// Define the 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 = 15;
self.update = function () {
self.y -= self.speed;
};
});
// Define the Car class
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for car
};
});
// Define the Gun class
var Gun = Container.expand(function () {
var self = Container.call(this);
var gunGraphics = self.attachAsset('gun', {
anchorX: 0.5,
anchorY: 0.5
});
self.shoot = function () {
// Shooting logic
};
self.update = function () {
// Update logic for gun
};
});
//<Assets used in the game will automatically appear here>
// Define the Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Update logic for hero
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var guns = [];
var cars = [];
var bullets = [];
var scoreTxt;
// Initialize the game elements
function initGame() {
// Create hero
hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 / 2;
// Create guns
for (var i = 0; i < 10; i++) {
var gun = new Gun();
gun.x = Math.random() * 2048;
gun.y = Math.random() * 2732;
guns.push(gun);
game.addChild(gun);
}
// Create cars
for (var i = 0; i < 20; i++) {
var car = new Car();
car.x = Math.random() * 2048;
car.y = Math.random() * 2732;
cars.push(car);
game.addChild(car);
}
// Create score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
}
// Handle game updates
game.update = function () {
// Update hero
hero.update();
// Update guns
for (var i = 0; i < guns.length; i++) {
guns[i].update();
}
// Update cars
for (var i = 0; i < cars.length; i++) {
cars[i].update();
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < -50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Fire bullet
if (LK.ticks % 30 == 0) {
var newBullet = new Bullet();
newBullet.x = hero.x;
newBullet.y = hero.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
};
// Handle touch events
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
// Initialize the game
initGame();