/**** * Classes ****/ //<Assets used in the game will automatically appear here> // 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 () { // Car movement logic if (self.y > 2732) { self.y = -carGraphics.height; } else { self.y += self.speed; } }; }); // Person class var Person = Container.expand(function () { var self = Container.call(this); var personGraphics = self.attachAsset('person', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Person movement logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var cars = []; var person; // Create and add person to the game person = game.addChild(new Person()); person.x = 2048 / 2; person.y = 2732 - 200; // Create and add cars to the game for (var i = 0; i < 5; i++) { var car = new Car(); car.x = Math.random() * 2048; car.y = Math.random() * 2732; cars.push(car); game.addChild(car); } // Handle touch/mouse move events game.move = function (x, y, obj) { person.x = x; person.y = y; }; // Update game logic game.update = function () { for (var i = 0; i < cars.length; i++) { cars[i].update(); if (person.intersects(cars[i])) { // Flash screen red for 1 second (1000ms) to show collision LK.effects.flashScreen(0xff0000, 1000); // Show game over. The game will be automatically paused while game over is showing. LK.showGameOver(); } } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// 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 () {
// Car movement logic
if (self.y > 2732) {
self.y = -carGraphics.height;
} else {
self.y += self.speed;
}
};
});
// Person class
var Person = Container.expand(function () {
var self = Container.call(this);
var personGraphics = self.attachAsset('person', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Person movement logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var cars = [];
var person;
// Create and add person to the game
person = game.addChild(new Person());
person.x = 2048 / 2;
person.y = 2732 - 200;
// Create and add cars to the game
for (var i = 0; i < 5; i++) {
var car = new Car();
car.x = Math.random() * 2048;
car.y = Math.random() * 2732;
cars.push(car);
game.addChild(car);
}
// Handle touch/mouse move events
game.move = function (x, y, obj) {
person.x = x;
person.y = y;
};
// Update game logic
game.update = function () {
for (var i = 0; i < cars.length; i++) {
cars[i].update();
if (person.intersects(cars[i])) {
// Flash screen red for 1 second (1000ms) to show collision
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
}
};