/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Ball class representing the cricket ball var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = 0; self.x = Math.random() * 2048; } }; }); // Bat class representing the cricket bat var Bat = Container.expand(function () { var self = Container.call(this); var batGraphics = self.attachAsset('bat', { anchorX: 0.5, anchorY: 0.5 }); self.down = function (x, y, obj) { self.x = x; self.y = y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var balls = []; var bat = game.addChild(new Bat()); bat.x = 1024; bat.y = 2500; // Create multiple balls for (var i = 0; i < 5; i++) { var ball = new Ball(); ball.x = Math.random() * 2048; ball.y = Math.random() * 1000; balls.push(ball); game.addChild(ball); } // Handle game updates game.update = function () { for (var i = 0; i < balls.length; i++) { balls[i].update(); if (balls[i].intersects(bat)) { balls[i].y = 0; balls[i].x = Math.random() * 2048; } } }; // Handle bat movement game.down = function (x, y, obj) { bat.x = x; bat.y = y; };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Ball class representing the cricket ball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
self.x = Math.random() * 2048;
}
};
});
// Bat class representing the cricket bat
var Bat = Container.expand(function () {
var self = Container.call(this);
var batGraphics = self.attachAsset('bat', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var balls = [];
var bat = game.addChild(new Bat());
bat.x = 1024;
bat.y = 2500;
// Create multiple balls
for (var i = 0; i < 5; i++) {
var ball = new Ball();
ball.x = Math.random() * 2048;
ball.y = Math.random() * 1000;
balls.push(ball);
game.addChild(ball);
}
// Handle game updates
game.update = function () {
for (var i = 0; i < balls.length; i++) {
balls[i].update();
if (balls[i].intersects(bat)) {
balls[i].y = 0;
balls[i].x = Math.random() * 2048;
}
}
};
// Handle bat movement
game.down = function (x, y, obj) {
bat.x = x;
bat.y = y;
};