/**** * Classes ****/ // The assets will be automatically created and loaded by the LK engine // 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 = 0; self.direction = 0; self.update = function () { self.x += Math.cos(self.direction) * self.speed; self.y += Math.sin(self.direction) * self.speed; }; }); // Define the Track class var Track = Container.expand(function () { var self = Container.call(this); var trackGraphics = self.attachAsset('track', { anchorX: 0.5, anchorY: 0.5 }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize the car and the track var car = game.addChild(new Car()); car.x = 2048 / 2; car.y = 2732 / 2; var track = game.addChild(new Track()); track.x = 2048 / 2; track.y = 2732 / 2; // Handle touch events to control the car game.down = function (x, y, obj) { var dx = x - car.x; var dy = y - car.y; car.direction = Math.atan2(dy, dx); car.speed = Math.sqrt(dx * dx + dy * dy) * 0.01; }; game.up = function (x, y, obj) { car.speed = 0; }; // Update the game state game.update = function () { // Check if the car is off the track if (!car.intersects(track)) { car.speed = 0; LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } };
/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine
// 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 = 0;
self.direction = 0;
self.update = function () {
self.x += Math.cos(self.direction) * self.speed;
self.y += Math.sin(self.direction) * self.speed;
};
});
// Define the Track class
var Track = Container.expand(function () {
var self = Container.call(this);
var trackGraphics = self.attachAsset('track', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize the car and the track
var car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2732 / 2;
var track = game.addChild(new Track());
track.x = 2048 / 2;
track.y = 2732 / 2;
// Handle touch events to control the car
game.down = function (x, y, obj) {
var dx = x - car.x;
var dy = y - car.y;
car.direction = Math.atan2(dy, dx);
car.speed = Math.sqrt(dx * dx + dy * dy) * 0.01;
};
game.up = function (x, y, obj) {
car.speed = 0;
};
// Update the game state
game.update = function () {
// Check if the car is off the track
if (!car.intersects(track)) {
car.speed = 0;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};