/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
// Check for goal
if (self.intersects(goal)) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
resetBall();
}
// Check for out of bounds
if (self.y < 0 || self.y > 2732 || self.x < 0 || self.x > 2048) {
resetBall();
}
};
});
// Assets will be automatically created and loaded during gameplay
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Player update logic if needed
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
var ball = game.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2732 - 300;
var goal = LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 100
});
game.addChild(goal);
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.down = function (x, y, obj) {
dragNode = ball;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
if (dragNode) {
var dx = x - ball.x;
var dy = y - ball.y;
ball.speedX = dx / 10;
ball.speedY = dy / 10;
}
dragNode = null;
};
function resetBall() {
ball.x = 2048 / 2;
ball.y = 2732 - 300;
ball.speedX = 0;
ball.speedY = 0;
}
game.update = function () {
// Update all game elements
player.update();
ball.update();
};