/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Basketball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('basketball', { anchorX: 0.5, anchorY: 0.5 }); self.speedY = 0; self.gravity = 0.5; self.bounce = -10; self.update = function () { self.speedY += self.gravity; self.y += self.speedY; if (self.y > 2732 - ballGraphics.height / 2) { self.y = 2732 - ballGraphics.height / 2; self.speedY = self.bounce; } }; self.tap = function () { self.speedY = self.bounce; }; return self; }); var Hoop = Container.expand(function () { var self = Container.call(this); var hoopGraphics = self.attachAsset('hoop', { anchorX: 0.5, anchorY: 0.5 }); self.speedX = -5; self.update = function () { self.x += self.speedX; if (self.x < -hoopGraphics.width / 2) { self.x = 2048 + hoopGraphics.width / 2; self.y = Math.random() * (2732 - 400) + 200; self.scored = false; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Initialize assets used in this game. Scale them according to what is needed for the game. game.setBackgroundColor(0x000000); var basketball = game.addChild(new Basketball()); basketball.x = 2048 / 4; basketball.y = 2732 / 2; basketball.scaleX = 1.5; basketball.scaleY = 1.5; var hoops = []; var hoop = game.addChild(new Hoop()); hoop.x = 2048; hoop.y = Math.random() * (2732 - 800) + 400; hoop.scaleX = 3; hoop.scaleY = 3; hoop.scored = false; hoops.push(hoop); var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var score = 0; game.down = function (x, y, obj) { basketball.tap(); }; game.update = function () { basketball.update(); for (var i = 0; i < hoops.length; i++) { hoops[i].update(); if (!hoops[i].scored && basketball.intersects(hoops[i])) { score += 10; scoreTxt.setText(score); LK.getSound('swish').play(); hoops[i].scored = true; } } if (basketball.y > 2732 - basketball.height / 2 || basketball.y < basketball.height / 2 || basketball.x < basketball.width / 2 || basketball.x > 2048 - basketball.width / 2) { LK.showGameOver(); } }; LK.playMusic('bgmusic');
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Basketball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.bounce = -10;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
if (self.y > 2732 - ballGraphics.height / 2) {
self.y = 2732 - ballGraphics.height / 2;
self.speedY = self.bounce;
}
};
self.tap = function () {
self.speedY = self.bounce;
};
return self;
});
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
self.update = function () {
self.x += self.speedX;
if (self.x < -hoopGraphics.width / 2) {
self.x = 2048 + hoopGraphics.width / 2;
self.y = Math.random() * (2732 - 400) + 200;
self.scored = false;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Initialize assets used in this game. Scale them according to what is needed for the game.
game.setBackgroundColor(0x000000);
var basketball = game.addChild(new Basketball());
basketball.x = 2048 / 4;
basketball.y = 2732 / 2;
basketball.scaleX = 1.5;
basketball.scaleY = 1.5;
var hoops = [];
var hoop = game.addChild(new Hoop());
hoop.x = 2048;
hoop.y = Math.random() * (2732 - 800) + 400;
hoop.scaleX = 3;
hoop.scaleY = 3;
hoop.scored = false;
hoops.push(hoop);
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var score = 0;
game.down = function (x, y, obj) {
basketball.tap();
};
game.update = function () {
basketball.update();
for (var i = 0; i < hoops.length; i++) {
hoops[i].update();
if (!hoops[i].scored && basketball.intersects(hoops[i])) {
score += 10;
scoreTxt.setText(score);
LK.getSound('swish').play();
hoops[i].scored = true;
}
}
if (basketball.y > 2732 - basketball.height / 2 || basketball.y < basketball.height / 2 || basketball.x < basketball.width / 2 || basketball.x > 2048 - basketball.width / 2) {
LK.showGameOver();
}
};
LK.playMusic('bgmusic');