/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Class for the basketball
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.update = function () {
self.y += self.speedY;
self.speedY += self.gravity;
if (self.y > 2732 - ballGraphics.height / 2) {
self.y = 2732 - ballGraphics.height / 2;
self.speedY = 0;
}
};
});
// Class for the hoop
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game elements
var basketball = game.addChild(new Basketball());
basketball.x = 2048 / 2;
basketball.y = 2732 - 200;
var hoop = game.addChild(new Hoop());
hoop.x = 2048 / 2;
hoop.y = 200;
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var dragNode = null;
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
dragNode = basketball;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
basketball.speedY = -15;
};
game.update = function () {
basketball.update();
if (basketball.intersects(hoop)) {
score += 1;
scoreTxt.setText(score);
basketball.y = 2732 - 200;
basketball.speedY = 0;
}
};