/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Car class to represent the player's car 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.maxSpeed = 10; self.acceleration = 0.2; self.brakeDeceleration = 0.5; self.turnSpeed = 2; self.update = function () { self.y -= self.speed; }; self.accelerate = function () { if (self.speed < self.maxSpeed) { self.speed += self.acceleration; } }; self.brake = function () { if (self.speed > 0) { self.speed -= self.brakeDeceleration; } }; self.turnLeft = function () { self.rotation -= self.turnSpeed * (Math.PI / 180); }; self.turnRight = function () { self.rotation += self.turnSpeed * (Math.PI / 180); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player's car var playerCar = new Car(); playerCar.x = 2048 / 2; playerCar.y = 2732 - 200; game.addChild(playerCar); // Event listeners for control buttons var accelerateButton = LK.getAsset('accelerateButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 - 100 }); game.addChild(accelerateButton); var brakeButton = LK.getAsset('brakeButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 - 50 }); game.addChild(brakeButton); var turnLeftButton = LK.getAsset('turnLeftButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 - 100, y: 2732 - 75 }); game.addChild(turnLeftButton); var turnRightButton = LK.getAsset('turnRightButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 + 100, y: 2732 - 75 }); game.addChild(turnRightButton); // Event handlers for buttons accelerateButton.down = function () { playerCar.accelerate(); }; brakeButton.down = function () { playerCar.brake(); }; turnLeftButton.down = function () { playerCar.turnLeft(); }; turnRightButton.down = function () { playerCar.turnRight(); }; // Game update loop game.update = function () { playerCar.update(); };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class to represent the player's car
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.maxSpeed = 10;
self.acceleration = 0.2;
self.brakeDeceleration = 0.5;
self.turnSpeed = 2;
self.update = function () {
self.y -= self.speed;
};
self.accelerate = function () {
if (self.speed < self.maxSpeed) {
self.speed += self.acceleration;
}
};
self.brake = function () {
if (self.speed > 0) {
self.speed -= self.brakeDeceleration;
}
};
self.turnLeft = function () {
self.rotation -= self.turnSpeed * (Math.PI / 180);
};
self.turnRight = function () {
self.rotation += self.turnSpeed * (Math.PI / 180);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player's car
var playerCar = new Car();
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
game.addChild(playerCar);
// Event listeners for control buttons
var accelerateButton = LK.getAsset('accelerateButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 - 100
});
game.addChild(accelerateButton);
var brakeButton = LK.getAsset('brakeButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 - 50
});
game.addChild(brakeButton);
var turnLeftButton = LK.getAsset('turnLeftButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2 - 100,
y: 2732 - 75
});
game.addChild(turnLeftButton);
var turnRightButton = LK.getAsset('turnRightButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2 + 100,
y: 2732 - 75
});
game.addChild(turnRightButton);
// Event handlers for buttons
accelerateButton.down = function () {
playerCar.accelerate();
};
brakeButton.down = function () {
playerCar.brake();
};
turnLeftButton.down = function () {
playerCar.turnLeft();
};
turnRightButton.down = function () {
playerCar.turnRight();
};
// Game update loop
game.update = function () {
playerCar.update();
};