+ 2

Please I need to make the ball bounce and move more smoothly across the screen.Your help would be really appreciated

https://code.sololearn.com/W4ejldM9ZQ3M/?ref=app

12th Feb 2018, 6:36 PM
osama okunbo
osama okunbo - avatar
3 ответов
+ 7
https://www.webpagefx.com/blog/web-design/bouncing-a-ball-around-with-html5-and-javascript/
13th Feb 2018, 2:08 AM
Vukan
Vukan - avatar
+ 6
Your actual code hangs out because of your 'callback' function, wich call twice the requestAnimationFrame() function at each call... appart from the two first times: At first "manual" call (callback()) lastTime is undefined, 'if' statement is not executed and lastTime get the value of millis (still undefined), then animation frame is requested a first time. When the second call occurs, lastTime is still undefined, 'if' statement is still not executed, lastTime get the value of millis (now not undefined), then another animation frame is requested. Since third call, lastTime is no more undefined, 'if' statement will be executed, inside wich your 'update' function is called AND animation frame is requested, BUT 'callback' code continue after 'if' statement, and another animation frame is requested, so you have initialized two callback branch at this moment, and you will do twice the 'fourth' call: in fact, 4th and 5th, and each next call will do it twice (after 4th and 5th, 4.1th, 4.2th, 5.1th and 5.2th are pending, and so on, very quickly growing). You need to remove the requestAnimationFrame() call from inside the 'if' statement: function callback(millis) { if (lastTime) { update((millis-lastTime) /1000); // requestAnimationFrame(callback); } lastTime = millis; requestAnimationFrame(callback); } Anyway, you cannot use 'id' name with spaces ("bounce ball"), and you must use some dash or another else valid char ("bounce_ball")... (middle dash is authorized in id/class name context -- "bounce-ball" -- but could be tricky in JS context, as it's not valid in JS identifier^^)
13th Feb 2018, 2:42 AM
visph
visph - avatar
0
Thanks guys...visph it's now working thanks alot really appreciate
13th Feb 2018, 5:09 AM
osama okunbo
osama okunbo - avatar