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
@@ -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