User prompt
in initGame, if !isDebug, set alpha of hoopTriggers to 0
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: handleHoopBorder is not defined' in or related to this line: 'handleHoopBorder();' Line Number: 341
Code edit (3 edits merged)
Please save this source code
User prompt
in main loop, call handleHoopBorder when ball touches a hoop border
User prompt
in hoop class, add 2 new objects hoopBorderLeft and hoopBorderRight
Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: ball.speedY.toString(...).toFixed is not a function' in or related to this line: 'scoreTxt.setText(ball.speedY.toString().toFixed(2));' Line Number: 319
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
now ballPassedAboveHoop should be set to true when ball touches the hoop's top Trigger
Code edit (2 edits merged)
Please save this source code
User prompt
in hoop class set hoopTopTrigger width to 220 and hoopBottomTrigger to 400
Code edit (6 edits merged)
Please save this source code
User prompt
Now add a bottom trigger in the hoop
Code edit (2 edits merged)
Please save this source code
User prompt
fix the fact that ball.intersects(hoop.hoopTriggerGraphics) is not triggered, maybe hoopTrigger needs to be an 'object' not just an asset...
Code edit (1 edits merged)
Please save this source code
User prompt
in main tick loop, call handleTopTrigger when ball intersects the hoop trigger
Code edit (1 edits merged)
Please save this source code
User prompt
add a hopptrigger in the hoop class
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of null (reading 'update')' in or related to this line: 'ball.update();' Line Number: 266
Code edit (1 edits merged)
Please save this source code
User prompt
Réinitialise le compteur de rebond à chaque fois que l'utilisateur touche l'écran.
/****
* Classes
****/
/* ********************************************************************************* */
/* ********************************** BALL CLASS *********************************** */
/* ********************************************************************************* */
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.wallBounceSpeedRatio = 0.95;
self.floorBounceRatio = 0.8;
self.gravityAcceleration = 1.2;
self.half = ballGraphics.width / 2;
self.isMoving = false;
self.launch = function (speedX, speedY) {
self.speedX = speedX;
self.speedY = speedY;
self.isMoving = true;
};
self.update = function () {
if (!self.isMoving) {
return;
}
self.x += self.speedX;
self.y += self.speedY;
// Make the basketball spin only when moving
if (self.speedX !== 0 || self.speedY !== 0) {
ballGraphics.rotation += 0.1 * Math.sign(self.speedX);
}
// Gradually reduce horizontal speed
// Apply friction to horizontal speed and limit to max speed
self.speedX *= 0.99;
self.speedX = Math.max(Math.min(self.speedX, maxSpeed), -maxSpeed); // Max speed limit
// Enhanced gravity effect with gradual vertical speed reduction and limit to max speed
self.speedY += self.gravityAcceleration;
self.speedY = Math.max(Math.min(self.speedY, maxSpeed), -maxSpeed); // Max speed limit
// Left and right boundaries
if (self.x <= 0 || self.x >= game.width) {
self.speedX *= -1 * self.wallBounceSpeedRatio;
bounceCounter += 1; // Increment bounce counter
}
// Top boundary
if (self.y <= 0 + self.half) {
self.y = 0 + self.half;
self.speedY *= -1 * self.wallBounceSpeedRatio;
bounceCounter += 1; // Increment bounce counter
// TODO : update bounce counter (x,y)
}
// Bottom boundary
if (ball.y > game.height - self.half) {
ball.y = game.height - self.half;
ball.speedY *= -1 * self.wallBounceSpeedRatio * self.floorBounceRatio;
bounceCounter += 1; // Increment bounce counter
}
// Left boundary
if (ball.x < 0 + self.half) {
ball.x = 0 + self.half;
ball.speedX *= -1 * self.wallBounceSpeedRatio;
bounceCounter += 1; // Increment bounce counter
}
if (ball.x > game.width - self.half) {
ball.x = game.width - self.half;
ball.speedX *= -1 * self.wallBounceSpeedRatio;
bounceCounter += 1; // Increment bounce counter
}
// Reset ball when in bottom and its speed is very low
if (ball.y > game.height * 0.75 && Math.abs(self.speedX) < 1 && Math.abs(self.speedY) < 1) {
self.reset();
}
};
self.reset = function () {
ballPassedAboveHoop = false;
ballPassedInsideHoop = false;
bounceCounter = 0; // Reset bounce counter
self.x = game.width / 2;
self.y = game.height - 300;
self.speedX = 0;
self.speedY = 0;
self.isMoving = false;
};
});
/* ********************************************************************************* */
/* ******************************** CONFETTI CLASS ********************************* */
/* ********************************************************************************* */
// Confetti class for creating a confetti effect
var Confetti = Container.expand(function () {
var self = Container.call(this);
var confettiColors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF];
var confettiPieces = [];
// Generate multiple confetti pieces
for (var i = 0; i < 200; i++) {
var color = confettiColors[Math.floor(Math.random() * confettiColors.length)];
var confettiPiece = self.attachAsset('butter', {
anchorX: 0.5,
anchorY: 0.5,
tint: color
});
confettiPiece.x = Math.random() * game.width * 2; // Spread across entire screen width
confettiPiece.y = Math.random() * game.height * 2 - game.height; // Spread across entire screen height
confettiPiece.scaleX = confettiPiece.scaleY = Math.random() * 0.5 + 0.5; // Random scale
confettiPieces.push(confettiPiece);
}
// Animate confetti pieces
self.animate = function () {
confettiPieces.forEach(function (piece) {
piece.y += Math.random() * 20 + 10; // Further increased fall speed
piece.rotation += Math.random() * 0.2 - 0.1; // Random rotation
// Remove piece if it goes off-screen
if (piece.y > game.height + 50) {
piece.destroy();
confettiPieces.splice(confettiPieces.indexOf(piece), 1);
}
});
// Stop animation and destroy confetti container if all pieces are gone
if (confettiPieces.length === 0) {
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.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
// Define hoopTrigger as a new Container object for better intersection detection
self.hoopTopTrigger = new Container();
var hoopTriggerGraphics = self.hoopTopTrigger.attachAsset('hoopTrigger', {
width: 200,
anchorX: 0.5,
anchorY: 0.5
});
// Position hoopTriggerGraphics inside hoopTrigger container
hoopTriggerGraphics.y = 0;
// Position hoopTrigger container relative to the hoop
self.hoopTopTrigger.y = -hoopGraphics.height / 2 + 40;
// Add hoopTrigger container as a child of Hoop
self.addChild(self.hoopTopTrigger);
// Define hoopBottomTrigger as a new Container object for better intersection detection
self.hoopBottomTrigger = new Container();
var hoopBottomTriggerGraphics = self.hoopBottomTrigger.attachAsset('hoopTrigger', {
width: 300,
anchorX: 0.5,
anchorY: 0.5
});
// Position hoopBottomTriggerGraphics inside hoopBottomTrigger container
hoopBottomTriggerGraphics.y = 0;
// Position hoopBottomTrigger container relative to the hoop
self.hoopBottomTrigger.y = -hoopGraphics.height / 2 + 150;
// Add hoopBottomTrigger container as a child of Hoop
self.addChild(self.hoopBottomTrigger);
// Define hoopBorderLeft as a new Container object for collision detection
self.hoopBorderLeft = new Container();
var hoopBorderLeftGraphics = self.hoopBorderLeft.attachAsset('hoopBorder', {
anchorX: 0.5,
anchorY: 0.5
});
// Position hoopBorderLeftGraphics inside hoopBorderLeft container
hoopBorderLeftGraphics.y = 0;
// Position hoopBorderLeft container relative to the hoop
self.hoopBorderLeft.x = -hoopGraphics.width / 2 + 20;
self.hoopBorderLeft.y = -hoopGraphics.height / 2 + 40;
// Add hoopBorderLeft container as a child of Hoop
self.addChild(self.hoopBorderLeft);
// Define hoopBorderRight as a new Container object for collision detection
self.hoopBorderRight = new Container();
var hoopBorderRightGraphics = self.hoopBorderRight.attachAsset('hoopBorder', {
anchorX: 0.5,
anchorY: 0.5
});
// Position hoopBorderRightGraphics inside hoopBorderRight container
hoopBorderRightGraphics.y = 0;
// Position hoopBorderRight container relative to the hoop
self.hoopBorderRight.x = hoopGraphics.width / 2 - 20;
self.hoopBorderRight.y = -hoopGraphics.height / 2 + 40;
// Add hoopBorderRight container as a child of Hoop
self.addChild(self.hoopBorderRight);
});
/****
* Initialize Game
****/
var game = new LK.Game({});
/****
* Game Code
****/
/* ********************************************************************************* */
/* ******************************* GAME VARIABLES ********************************** */
/* ********************************************************************************* */
var isGameRunning = false;
var isHandlingScore = false;
var bounceCounter = 0;
var maxSpeed = 100;
var ballPassedAboveHoop = false;
var ballPassedInsideHoop = false;
var timerSeconds = 600; // Set the initial timer value in seconds
var ball = null;
var hoop = null;
var score = 0;
var startPosition = null;
var isDebug = false;
// UI
var background = null;
var scoreTxt = null;
var timerTxt = null;
/* ********************************************************************************* */
/* ******************************* INPUT HANDLERS ********************************** */
/* ********************************************************************************* */
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
startPosition = pos;
bounceCounter = 0; // Reset bounce counter when the user touches the screen
});
game.on('up', function (obj) {
if (startPosition) {
var endPosition = obj.event.getLocalPosition(game);
var speedX = Math.max(Math.min((endPosition.x - startPosition.x) * 0.1, maxSpeed), -maxSpeed);
var speedY = Math.max(Math.min((endPosition.y - startPosition.y) * 0.1, maxSpeed), -maxSpeed);
ball.launch(speedX, speedY);
startPosition = null;
}
});
/* ********************************************************************************* */
/* ********************************** GAME FUNCTIONS ************************************ */
/* ********************************************************************************* */
function initGame() {
// Background
var background = LK.getAsset('background', {
anchorX: 0.0,
anchorY: 0.0,
x: 0,
y: 0
});
game.addChild(background);
// Score UI
scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#006400" // Changed color to dark green
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Timer UI
timerTxt = new Text2(timerSeconds.toString(), {
size: 150,
fill: "#006400"
});
timerTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(timerTxt);
// Update the timer every second
var timerInterval = LK.setInterval(function () {
timerSeconds -= 1; // Decrement the timer by one second
timerTxt.setText(timerSeconds.toString()); // Update the timer display
if (timerSeconds <= 0) {
LK.clearInterval(timerInterval); // Stop the timer when it reaches 0
LK.setScore(score); // Save the final score using the platform's tool
LK.showGameOver(); // Show game over screen
}
}, 1000); // Set the interval to update every 1000ms (1 second)
ball = game.addChild(new Ball());
ball.reset();
hoop = game.addChild(new Hoop());
if (!isDebug) {
hoop.hoopTopTrigger.alpha = 0;
hoop.hoopBottomTrigger.alpha = 0;
}
hoop.setPosition(game.width / 2, 1024); // Position the hoop at the top center
isGameRunning = true;
}
function handleTopTrigger() {
console.log("Top trigger. speed", ball.speedY);
if (ball.speedY > 0) {
ballPassedAboveHoop = true;
}
}
function handleBottomTrigger() {
console.log("Bottom trigger");
if (ballPassedAboveHoop) {
ballPassedInsideHoop = true;
handleScore();
} else {
console.log("touch hoop bottom", ball.speedY);
ball.speedY *= -0.98;
ballPassedAboveHoop = false;
ballPassedInsideHoop = false;
}
}
function handleHoopBorder(border) {
console.log("handleHoopBorder ");
// Calcul du vecteur normal
var dx = ball.x - border.x;
var dy = ball.y - border.y;
var normal = normalize({
x: dx,
y: dy
});
// Calcul du vecteur de réflexion
var dot = ball.speedX * normal.x + ball.speedY * normal.y;
var reflection = {
x: ball.speedX - 2 * dot * normal.x,
y: ball.speedY - 2 * dot * normal.y
};
// Mise Ă jour de la vitesse de la balle
ball.speedX = reflection.x * ball.wallBounceSpeedRatio;
ball.speedY = reflection.y * ball.wallBounceSpeedRatio;
// Mise Ă jour du compteur de rebonds
bounceCounter += 1;
}
// Fonction pour normaliser un vecteur
function normalize(vector) {
var magnitude = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
return {
x: vector.x / magnitude,
y: vector.y / magnitude
};
}
function handleScore() {
if (isHandlingScore) {
return;
}
console.log("handleScore...");
isHandlingScore = true;
score += 1 + bounceCounter; // Add bounce counter to score
scoreTxt.setText(score.toString());
//ball.reset();
ballPassedAboveHoop = false; // Reset the condition after scoring
ballPassedInsideHoop = false;
// Create and add confetti effect to the game
var confetti = game.addChild(new Confetti());
confetti.x = 0; // Position confetti at the hoop's position
confetti.y = 0;
LK.on('tick', function () {
confetti.animate(); // Animate confetti
});
// Initiate gradual movement of the hoop to a new random position within the game boundaries
var targetX = Math.random() * (game.width - hoop.width) + hoop.width / 2;
var targetY = Math.max(Math.random() * (game.height / 2) + 100, 780); // Ensure hoop's Y position does not go below 780
var moveHoopInterval = LK.setInterval(function () {
hoop.x += (targetX - hoop.x) * 0.05; // Move 5% of the distance per tick
hoop.y += (targetY - hoop.y) * 0.05; // Move 5% of the distance per tick
// Check if the hoop is close enough to the target position to stop
if (Math.abs(hoop.x - targetX) < 1 && Math.abs(hoop.y - targetY) < 1) {
hoop.setPosition(targetX, targetY); // Ensure hoop is exactly at target position
LK.clearInterval(moveHoopInterval); // Stop the interval
console.log("Ok can handle score...");
isHandlingScore = false;
}
}, 16); // Run every 16ms (~60FPS)
}
/* ********************************************************************************* */
/* ********************************** MAIN LOOP ************************************ */
/* ********************************************************************************* */
LK.on('tick', function () {
if (!isGameRunning) {
return;
}
ball.update();
if (ball.intersects(hoop.hoopBorderLeft)) {
handleHoopBorder(hoop.hoopBorderLeft);
}
if (ball.intersects(hoop.hoopBorderRight)) {
handleHoopBorder(hoop.hoopBorderRight);
}
if (!ballPassedInsideHoop && !ballPassedAboveHoop && ball.intersects(hoop.hoopTopTrigger)) {
handleTopTrigger();
}
if (!ballPassedInsideHoop && ball.intersects(hoop.hoopBottomTrigger)) {
handleBottomTrigger();
}
//scoreTxt.setText(ball.speedY.toFixed(2));
});
initGame(); ===================================================================
--- original.js
+++ change.js
@@ -42,9 +42,8 @@
// Left and right boundaries
if (self.x <= 0 || self.x >= game.width) {
self.speedX *= -1 * self.wallBounceSpeedRatio;
bounceCounter += 1; // Increment bounce counter
- scoreTxt.setText(score.toString()); // Update score display
}
// Top boundary
if (self.y <= 0 + self.half) {
self.y = 0 + self.half;
@@ -138,9 +137,9 @@
};
// Define hoopTrigger as a new Container object for better intersection detection
self.hoopTopTrigger = new Container();
var hoopTriggerGraphics = self.hoopTopTrigger.attachAsset('hoopTrigger', {
- width: 220,
+ width: 200,
anchorX: 0.5,
anchorY: 0.5
});
// Position hoopTriggerGraphics inside hoopTrigger container
@@ -151,16 +150,16 @@
self.addChild(self.hoopTopTrigger);
// Define hoopBottomTrigger as a new Container object for better intersection detection
self.hoopBottomTrigger = new Container();
var hoopBottomTriggerGraphics = self.hoopBottomTrigger.attachAsset('hoopTrigger', {
- width: 220,
+ width: 300,
anchorX: 0.5,
anchorY: 0.5
});
// Position hoopBottomTriggerGraphics inside hoopBottomTrigger container
hoopBottomTriggerGraphics.y = 0;
// Position hoopBottomTrigger container relative to the hoop
- self.hoopBottomTrigger.y = -hoopGraphics.height / 2 + 140;
+ self.hoopBottomTrigger.y = -hoopGraphics.height / 2 + 150;
// Add hoopBottomTrigger container as a child of Hoop
self.addChild(self.hoopBottomTrigger);
// Define hoopBorderLeft as a new Container object for collision detection
self.hoopBorderLeft = new Container();
@@ -201,17 +200,19 @@
/* ********************************************************************************* */
/* ******************************* GAME VARIABLES ********************************** */
/* ********************************************************************************* */
var isGameRunning = false;
+var isHandlingScore = false;
var bounceCounter = 0;
var maxSpeed = 100;
var ballPassedAboveHoop = false;
var ballPassedInsideHoop = false;
-var timerSeconds = 60; // Set the initial timer value in seconds
+var timerSeconds = 600; // Set the initial timer value in seconds
var ball = null;
var hoop = null;
var score = 0;
var startPosition = null;
+var isDebug = false;
// UI
var background = null;
var scoreTxt = null;
var timerTxt = null;
@@ -270,8 +271,12 @@
}, 1000); // Set the interval to update every 1000ms (1 second)
ball = game.addChild(new Ball());
ball.reset();
hoop = game.addChild(new Hoop());
+ if (!isDebug) {
+ hoop.hoopTopTrigger.alpha = 0;
+ hoop.hoopBottomTrigger.alpha = 0;
+ }
hoop.setPosition(game.width / 2, 1024); // Position the hoop at the top center
isGameRunning = true;
}
function handleTopTrigger() {
@@ -291,18 +296,46 @@
ballPassedAboveHoop = false;
ballPassedInsideHoop = false;
}
}
-function handleHoopBorder() {
- console.log("Ball touched hoop border");
- // Reverse ball's horizontal speed and apply a slight reduction
- ball.speedX *= -0.95;
- ball.speedY *= -0.95;
+function handleHoopBorder(border) {
+ console.log("handleHoopBorder ");
+ // Calcul du vecteur normal
+ var dx = ball.x - border.x;
+ var dy = ball.y - border.y;
+ var normal = normalize({
+ x: dx,
+ y: dy
+ });
+ // Calcul du vecteur de réflexion
+ var dot = ball.speedX * normal.x + ball.speedY * normal.y;
+ var reflection = {
+ x: ball.speedX - 2 * dot * normal.x,
+ y: ball.speedY - 2 * dot * normal.y
+ };
+ // Mise Ă jour de la vitesse de la balle
+ ball.speedX = reflection.x * ball.wallBounceSpeedRatio;
+ ball.speedY = reflection.y * ball.wallBounceSpeedRatio;
+ // Mise Ă jour du compteur de rebonds
+ bounceCounter += 1;
}
+// Fonction pour normaliser un vecteur
+function normalize(vector) {
+ var magnitude = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
+ return {
+ x: vector.x / magnitude,
+ y: vector.y / magnitude
+ };
+}
function handleScore() {
+ if (isHandlingScore) {
+ return;
+ }
+ console.log("handleScore...");
+ isHandlingScore = true;
score += 1 + bounceCounter; // Add bounce counter to score
scoreTxt.setText(score.toString());
- ball.reset();
+ //ball.reset();
ballPassedAboveHoop = false; // Reset the condition after scoring
ballPassedInsideHoop = false;
// Create and add confetti effect to the game
var confetti = game.addChild(new Confetti());
@@ -320,8 +353,10 @@
// Check if the hoop is close enough to the target position to stop
if (Math.abs(hoop.x - targetX) < 1 && Math.abs(hoop.y - targetY) < 1) {
hoop.setPosition(targetX, targetY); // Ensure hoop is exactly at target position
LK.clearInterval(moveHoopInterval); // Stop the interval
+ console.log("Ok can handle score...");
+ isHandlingScore = false;
}
}, 16); // Run every 16ms (~60FPS)
}
/* ********************************************************************************* */
@@ -331,26 +366,19 @@
if (!isGameRunning) {
return;
}
ball.update();
- if (ball.intersects(hoop.hoopBorderLeft) || ball.intersects(hoop.hoopBorderRight)) {
- handleHoopBorder();
+ if (ball.intersects(hoop.hoopBorderLeft)) {
+ handleHoopBorder(hoop.hoopBorderLeft);
}
+ if (ball.intersects(hoop.hoopBorderRight)) {
+ handleHoopBorder(hoop.hoopBorderRight);
+ }
if (!ballPassedInsideHoop && !ballPassedAboveHoop && ball.intersects(hoop.hoopTopTrigger)) {
handleTopTrigger();
}
if (!ballPassedInsideHoop && ball.intersects(hoop.hoopBottomTrigger)) {
handleBottomTrigger();
}
- scoreTxt.setText(ball.speedY.toFixed(2));
- /*else if (ball.intersects(hoop)) {
- if (ballPassedAboveHoop) {
- console.log("touch hoop top");
- handleScore();
- } else if (ball.speedY < 0) {
- console.log("touch hoop bottom");
- ball.speedY *= -0.98;
- }
- }
- */
+ //scoreTxt.setText(ball.speedY.toFixed(2));
});
initGame();
\ No newline at end of file