/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Basketball class
var Basketball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = -10;
self.update = function () {
self.y += self.speedY;
if (self.y < -50) {
self.destroy();
}
};
});
// Hoop class
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Hoop logic if needed
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var basketballs = [];
var hoop = game.addChild(new Hoop());
hoop.x = 2048 / 2;
hoop.y = 500;
var dragNode = null;
// Function to handle move events
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}
// Mouse or touch down on the game
game.down = function (x, y, obj) {
for (var i = 0; i < basketballs.length; i++) {
if (basketballs[i].intersects({
x: x,
y: y,
width: 1,
height: 1
})) {
dragNode = basketballs[i];
return;
}
}
var newBasketball = new Basketball();
newBasketball.x = x;
newBasketball.y = y;
basketballs.push(newBasketball);
game.addChild(newBasketball);
dragNode = newBasketball;
};
// Mouse or touch move on game object
game.move = handleMove;
// Mouse or touch up on game object
game.up = function (x, y, obj) {
dragNode = null;
};
// Update game logic
game.update = function () {
for (var i = basketballs.length - 1; i >= 0; i--) {
if (basketballs[i].intersects(hoop)) {
score += 1;
scoreTxt.setText(score);
basketballs[i].destroy();
basketballs.splice(i, 1);
} else if (basketballs[i].y < -50) {
basketballs[i].destroy();
basketballs.splice(i, 1);
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,50 +1,50 @@
-/****
+/****
* Classes
-****/
+****/
//<Assets used in the game will automatically appear here>
// Basketball class
var Basketball = Container.expand(function () {
- var self = Container.call(this);
- var ballGraphics = self.attachAsset('basketball', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speedY = -10;
- self.update = function () {
- self.y += self.speedY;
- if (self.y < -50) {
- self.destroy();
- }
- };
+ var self = Container.call(this);
+ var ballGraphics = self.attachAsset('basketball', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speedY = -10;
+ self.update = function () {
+ self.y += self.speedY;
+ if (self.y < -50) {
+ self.destroy();
+ }
+ };
});
// Hoop class
var Hoop = Container.expand(function () {
- var self = Container.call(this);
- var hoopGraphics = self.attachAsset('hoop', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.update = function () {
- // Hoop logic if needed
- };
+ var self = Container.call(this);
+ var hoopGraphics = self.attachAsset('hoop', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Hoop logic if needed
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Initialize variables
var score = 0;
var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+ size: 150,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var basketballs = [];
@@ -53,34 +53,49 @@
hoop.y = 500;
var dragNode = null;
// Function to handle move events
function handleMove(x, y, obj) {
- if (dragNode) {
- dragNode.x = x;
- dragNode.y = y;
- }
+ if (dragNode) {
+ dragNode.x = x;
+ dragNode.y = y;
+ }
}
// Mouse or touch down on the game
game.down = function (x, y, obj) {
- var newBasketball = new Basketball();
- newBasketball.x = x;
- newBasketball.y = y;
- basketballs.push(newBasketball);
- game.addChild(newBasketball);
+ for (var i = 0; i < basketballs.length; i++) {
+ if (basketballs[i].intersects({
+ x: x,
+ y: y,
+ width: 1,
+ height: 1
+ })) {
+ dragNode = basketballs[i];
+ return;
+ }
+ }
+ var newBasketball = new Basketball();
+ newBasketball.x = x;
+ newBasketball.y = y;
+ basketballs.push(newBasketball);
+ game.addChild(newBasketball);
+ dragNode = newBasketball;
};
// Mouse or touch move on game object
game.move = handleMove;
// Mouse or touch up on game object
game.up = function (x, y, obj) {
- dragNode = null;
+ dragNode = null;
};
// Update game logic
game.update = function () {
- for (var i = basketballs.length - 1; i >= 0; i--) {
- if (basketballs[i].intersects(hoop)) {
- score += 1;
- scoreTxt.setText(score);
- basketballs[i].destroy();
- basketballs.splice(i, 1);
- }
- }
+ for (var i = basketballs.length - 1; i >= 0; i--) {
+ if (basketballs[i].intersects(hoop)) {
+ score += 1;
+ scoreTxt.setText(score);
+ basketballs[i].destroy();
+ basketballs.splice(i, 1);
+ } else if (basketballs[i].y < -50) {
+ basketballs[i].destroy();
+ basketballs.splice(i, 1);
+ }
+ }
};
\ No newline at end of file