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.
===================================================================
--- original.js
+++ change.js
@@ -1,82 +1,86 @@
/****
* Classes
****/
-// Assets will be automatically generated based on usage in the code.
-// Ball class for handling the basketball's behavior
+/* ********************************************************************************* */
+/* ********************************** 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.96;
+ 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) {
- 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;
- }
- // 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 += 1.2;
- //self.speedY *= 0.99;
- self.speedY = Math.max(Math.min(self.speedY, maxSpeed), -maxSpeed); // Max speed limit
- // Check for boundary collisions, reverse speed, increment bounce counter, and lose points
- if (self.x <= 0 || self.x >= game.width) {
- self.speedX *= -1;
- bounceCounter += 1; // Increment bounce counter
- scoreTxt.setText(score.toString()); // Update score display
- }
- if (self.y <= 0) {
- self.speedY *= -0.98;
- bounceCounter += 1; // Increment bounce counter
- scoreTxt.setText(score.toString()); // Update score display
- }
- // Check if the ball has passed above the hoop and set ballPassedAboveHoop to true
- if (self.y < hoop.y - hoop.height / 2 - 200 && !ballPassedAboveHoop) {
- ballPassedAboveHoop = true;
- }
- // Set ballPassedAboveHoop to false when the ball passes under the hoop (+ its height)
- if (self.y > hoop.y + hoop.height / 2) {
- ballPassedAboveHoop = false;
- }
- // Reset ball when its speed is very low
- if (Math.abs(self.speedX) < 1 && Math.abs(self.speedY) < 1) {
- self.reset();
- }
- // Allow the ball to fall off the bottom without reversing speed or losing points
- // If the ball touches the bottom boundary and score is 0, trigger game over
- // Check if the ball is coming from below the hoop and make it bounce
- if (self.y >= game.height) {
- // || self.intersects(hoop) && self.speedY > 0) {
- self.speedY *= -0.5;
- }
- if (ball.y > game.height - 50) {
- ball.y = game.height - 50;
- ball.speedY *= -0.75;
- }
- if (ball.x < 50) {
- ball.x = 50;
- ball.speedX *= -0.95;
- }
- if (ball.x > game.width - 50) {
- ball.x = game.width - 50;
- ball.speedX *= -0.95;
- }
+ 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) * Math.sign(self.speedY);
+ }
+ // Gradually reduce horizontal speed
+ // Apply friction to horizontal speed and limit to max speed
+ self.speedX *= self.wallBounceSpeedRatio;
+ 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
+ scoreTxt.setText(score.toString()); // Update score display
+ }
+ // 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;
+ 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
+ }
+ // Check if the ball has passed above the hoop and set ballPassedAboveHoop to true
+ if (self.y < hoop.y - hoop.height / 2 - 200 && !ballPassedAboveHoop) {
+ ballPassedAboveHoop = true;
+ }
+ // Set ballPassedAboveHoop to false when the ball passes under the hoop (+ its height)
+ if (self.y > hoop.y + hoop.height / 2) {
+ ballPassedAboveHoop = false;
+ }
+ // Reset ball when its speed is very low
+ if (Math.abs(self.speedX) < 1 && Math.abs(self.speedY) < 1) {
+ self.reset();
+ }
};
self.reset = function () {
ballPassedAboveHoop = false;
bounceCounter = 0; // Reset bounce counter
@@ -86,8 +90,11 @@
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];
@@ -121,9 +128,11 @@
self.destroy();
}
};
});
-// Hoop class for handling the basketball hoop's behavior
+/* ********************************************************************************* */
+/* ********************************** HOOP CLASS *********************************** */
+/* ********************************************************************************* */
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
@@ -142,28 +151,81 @@
/****
* Game Code
****/
-// Initialize bounce counter variable
+/* ********************************************************************************* */
+/* ******************************* GAME VARIABLES ********************************** */
+/* ********************************************************************************* */
var bounceCounter = 0;
-// Initialize timer variables
+var maxSpeed = 100;
+var ballPassedAboveHoop = false;
var timerSeconds = 60; // Set the initial timer value in seconds
-var timerTxt = new Text2(timerSeconds.toString(), {
- size: 150,
- fill: "#000000" // Timer text color set to black
+var ball = null;
+var hoop = null;
+var score = 0;
+var startPosition = null;
+// 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
});
-timerTxt.anchor.set(1, 0); // Anchor the timer text to the top right
-LK.gui.topRight.addChild(timerTxt); // Add the timer text to the top-right GUI overlay
-// 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
+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;
}
-}, 1000); // Set the interval to update every 1000ms (1 second)
+});
+/* ********************************************************************************* */
+/* ********************************** 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());
+ hoop.setPosition(game.width / 2, 1024); // Position the hoop at the top center
+}
function handleScore() {
score += 1 + bounceCounter; // Add bounce counter to score
scoreTxt.setText(score.toString());
ball.reset();
@@ -187,47 +249,15 @@
LK.clearInterval(moveHoopInterval); // Stop the interval
}
}, 16); // Run every 16ms (~60FPS)
}
-var maxSpeed = 100;
-// Global variable to track if the ball has passed above the hoop
-var ballPassedAboveHoop = false;
-// Display the background asset
-var background = LK.getAsset('background', {
- anchorX: 0.0,
- anchorY: 0.0,
- x: 0,
- y: 0
-});
-game.addChild(background);
-var ball = game.addChild(new Ball());
-ball.reset();
-var hoop = game.addChild(new Hoop());
-hoop.setPosition(game.width / 2, 1024); // Position the hoop at the top center
-var score = 0;
-var 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);
-var startPosition = null;
-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;
- }
-});
+/* ********************************************************************************* */
+/* ********************************** MAIN LOOP ************************************ */
+/* ********************************************************************************* */
LK.on('tick', function () {
- ball.update();
+ if (ball) {
+ ball.update();
+ }
if (ball.intersects(hoop)) {
if (ballPassedAboveHoop) {
handleScore();
} else if (ball.speedY < 0) {