0
I don't understand what's problem with my code
I tried many ways to solve it but I can't there is a problem in my JavaScript code first time try to create a bouncing Ball game but I can't what's the problem I don't understand can you please help me guys https://code.sololearn.com/WIohd6135nOu/?ref=app
7 Réponses
+ 1
I've updated the code as you asked me in message. check this:
https://code.sololearn.com/WX9BPlg0CSNG/?ref=app
+ 2
Step 1: delete 6 of your 7 duplicate posts. Gonna assume there was a technical glitch there since all the posts are identical.
+ 2
Step 2: there are a lot of problems here, but you can start by just wrapping everything in one window.onload (this will require some } hunting) and then following the error messages.
+ 2
A bit better, but now combine the two window.onload into one, otherwise they can't talk to each other. Then you'll get useful error messages.
+ 1
update your javascript with the following:
// Get the canvas and button elements
var canvas = document.getElementById("canvas");
var jumpButton = document.getElementById("jump");
// Set up the canvas context
var context = canvas.getContext("2d");
context.fillStyle = "#ff0000";
// Set up initial ball position and velocity
var ball = { x: 100, y: 100, radius: 20, dy: 0, gravity: 0.5 };
// Function to update the ball position
function updateBall() {
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
context.fill();
// Apply gravity to the ball
ball.y += ball.dy;
ball.dy += ball.gravity;
// Bounce the ball off the ground
if (ball.y + ball.radius > canvas.height) {
ball.y = canvas.height - ball.radius;
ball.dy *= -0.8; // Reverse the velocity and add some damping
}
}
// Function to handle the jump button click event
function handleJump() {
// Increase the ball's upward velocity
ball.dy = -10;
}
// Add event listener to the jump button
jumpButton.addEventListener("click", handleJump);
// Update the ball position on each frame
function animate() {
requestAnimationFrame(animate);
updateBall();
}
// Start the animation
animate();
0
Orion thank you so much
0
You are master