/****
* 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 = 0;
self.angle = 0;
self.driftAngle = 0;
self.update = function () {
self.x += Math.cos(self.angle) * self.speed;
self.y += Math.sin(self.angle) * self.speed;
self.rotation = self.angle + self.driftAngle;
};
self.drift = function (driftAmount) {
self.driftAngle = driftAmount;
};
});
// 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 variables
var car;
var track;
var scoreTxt;
var score = 0;
var isDrifting = false;
// Initialize game elements
function initGame() {
// Create track
track = game.addChild(new Track());
track.x = 2048 / 2;
track.y = 2732 / 2;
// Create car
car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2732 / 2;
// Create score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
}
// Update score
function updateScore() {
score += 1;
scoreTxt.setText('Score: ' + score);
}
// Handle touch events
game.down = function (x, y, obj) {
isDrifting = true;
car.drift(0.1);
};
game.up = function (x, y, obj) {
isDrifting = false;
car.drift(0);
};
// Game update loop
game.update = function () {
car.update();
if (isDrifting) {
updateScore();
}
};
// Initialize the game
initGame();