/**** * Classes ****/ var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for the shooting ball var ShootingBall = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('shootingBall', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () {}; self.down = function (x, y, obj) { var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y; bullets.push(bullet); game.addChild(bullet); }; }); // Class for the small balls var SmallBall = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('smallBall', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = 0; // Reset position if it goes off screen } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var shootingBall = game.addChild(new ShootingBall()); shootingBall.x = 2048 / 2; shootingBall.y = 2732 - 200; var smallBalls = []; for (var i = 0; i < 5; i++) { var smallBall = new SmallBall(); smallBall.x = Math.random() * 2048; smallBall.y = Math.random() * 2732; smallBalls.push(smallBall); game.addChild(smallBall); } var dragNode = null; //Declare method for handling move events on game. function handleMove(x, y, obj) { //Get event position in relation to game object if (dragNode) { dragNode.x = x; } } //Mouse or touch move on game object. LK returns complex event objects. Please be aware of this. game.move = handleMove; //Allow dragging of the shootingBall to start from anywhere. game.down = function (x, y, obj) { //Set drag node to shootingBall. dragNode = shootingBall; //Also call move handler right away to make effect instant. handleMove(x, obj.global.y, obj); }; //Mouse or touch up on game object game.up = function (x, y, obj) { dragNode = null; }; var bullets = []; game.update = function () { for (var i = smallBalls.length - 1; i >= 0; i--) { if (shootingBall.intersects(smallBalls[i])) { LK.effects.flashObject(smallBalls[i], 0xff0000, 1000); // Add explosion effect smallBalls[i].destroy(); smallBalls.splice(i, 1); break; } for (var j = bullets.length - 1; j >= 0; j--) { if (bullets[j].intersects(smallBalls[i])) { LK.effects.flashObject(smallBalls[i], 0xff0000, 1000); // Add explosion effect smallBalls[i].destroy(); smallBalls.splice(i, 1); bullets[j].destroy(); bullets.splice(j, 1); break; } } } for (var i = smallBalls.length; i < 10; i++) { // Re-spawn small ball var smallBall = new SmallBall(); smallBall.x = Math.random() * 2048; smallBall.y = Math.random() * 2732; smallBalls.push(smallBall); game.addChild(smallBall); // Change the color of the small ball smallBall.tint = Math.random() * 0xFFFFFF; } }; // This code block has been moved inside the 'ShootingBall' class.
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the shooting ball
var ShootingBall = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('shootingBall', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {};
self.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
bullets.push(bullet);
game.addChild(bullet);
};
});
// Class for the small balls
var SmallBall = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('smallBall', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0; // Reset position if it goes off screen
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var shootingBall = game.addChild(new ShootingBall());
shootingBall.x = 2048 / 2;
shootingBall.y = 2732 - 200;
var smallBalls = [];
for (var i = 0; i < 5; i++) {
var smallBall = new SmallBall();
smallBall.x = Math.random() * 2048;
smallBall.y = Math.random() * 2732;
smallBalls.push(smallBall);
game.addChild(smallBall);
}
var dragNode = null;
//Declare method for handling move events on game.
function handleMove(x, y, obj) {
//Get event position in relation to game object
if (dragNode) {
dragNode.x = x;
}
}
//Mouse or touch move on game object. LK returns complex event objects. Please be aware of this.
game.move = handleMove;
//Allow dragging of the shootingBall to start from anywhere.
game.down = function (x, y, obj) {
//Set drag node to shootingBall.
dragNode = shootingBall;
//Also call move handler right away to make effect instant.
handleMove(x, obj.global.y, obj);
};
//Mouse or touch up on game object
game.up = function (x, y, obj) {
dragNode = null;
};
var bullets = [];
game.update = function () {
for (var i = smallBalls.length - 1; i >= 0; i--) {
if (shootingBall.intersects(smallBalls[i])) {
LK.effects.flashObject(smallBalls[i], 0xff0000, 1000); // Add explosion effect
smallBalls[i].destroy();
smallBalls.splice(i, 1);
break;
}
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j].intersects(smallBalls[i])) {
LK.effects.flashObject(smallBalls[i], 0xff0000, 1000); // Add explosion effect
smallBalls[i].destroy();
smallBalls.splice(i, 1);
bullets[j].destroy();
bullets.splice(j, 1);
break;
}
}
}
for (var i = smallBalls.length; i < 10; i++) {
// Re-spawn small ball
var smallBall = new SmallBall();
smallBall.x = Math.random() * 2048;
smallBall.y = Math.random() * 2732;
smallBalls.push(smallBall);
game.addChild(smallBall);
// Change the color of the small ball
smallBall.tint = Math.random() * 0xFFFFFF;
}
};
// This code block has been moved inside the 'ShootingBall' class.