Code edit (1 edits merged)
Please save this source code
User prompt
in popupMultiplier, don't use scale, use width and height
Code edit (1 edits merged)
Please save this source code
User prompt
call popupMultiplier(value, x, y) at each bounce
Code edit (2 edits merged)
Please save this source code
User prompt
in popupMultiplier, popup the x2 asset at the position x,y. start at with,heigth 1x1 and make it grow until 1024x1024
User prompt
in popupMultiplier, check if value is in possibleValues, if not set it to 999;
Code edit (5 edits merged)
Please save this source code
User prompt
flash the ball at each bounce
User prompt
flash the screen at each bounce
Code edit (7 edits merged)
Please save this source code
User prompt
move the hoop after a shoot even if not scored
Code edit (1 edits merged)
Please save this source code
Code edit (9 edits merged)
Please save this source code
User prompt
don't accept other touch while ball is moving
Code edit (5 edits merged)
Please save this source code
User prompt
reduce ball speed when launched
User prompt
reduce ball speed
Code edit (6 edits merged)
Please save this source code
User prompt
in LK.on('tick', ), after ball.update();, calculate distance between ball and hoop
Code edit (1 edits merged)
Please save this source code
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
===================================================================
--- 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