+ 2

Problem with the code

Game Development with JavaScript course. Please tell me why the code is not working? window.onload = function(){ var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); var x = 300; var y = 350; var t = Date.now; var speed = 25; let count = 0; document.onmousedown = function(){ count += 1; y -= 25; speed = 25; } document.onkeydown = function(){ count += 1; y -= 25; speed = 25; } document.ontouchstart = function(){ count += 1; y -= 25; speed = 25; } function draw(){ var timePassed = (Date.now() - t) / 1000; t = Date.now(); context.clearRect(0, 0, 600, 400); context.font = "25px Arial"; context.fillStyle = "#ffffff"; context.fillText("Count: " + count, 20, 30); context.beginPath(); context.arc(x, y, 50, 0, 2 * Math.PI); context.fillStyle = "red"; context.fill(); if(y <= 350){ speed += 50 * timePassed; y += speed * timePassed; //when I add this line the circle disappears and the code doesn't work properly } if(y > 350){ count = 0 } window.requestAnimationFrame(draw); } draw(); }

22nd Apr 2022, 3:12 PM
Mitiai (Dmytro) Bondur
Mitiai (Dmytro) Bondur - avatar
6 Answers
+ 9
Problem solved. Don't thank me 😉.
22nd Apr 2022, 7:22 PM
🇺🇦 Drake CDR 🇺🇦
🇺🇦 Drake CDR 🇺🇦 - avatar
+ 4
Please LINK your code instead of copying into the description
22nd Apr 2022, 3:30 PM
Lisa
Lisa - avatar
+ 2
ChillPill 🌶 I think you did the tutorial from the Game Dev course and know what it is supposed to do?
22nd Apr 2022, 5:10 PM
Lisa
Lisa - avatar
+ 2
Oh, cool that you found the issue! :)
22nd Apr 2022, 7:07 PM
Lisa
Lisa - avatar
22nd Apr 2022, 3:46 PM
Mitiai (Dmytro) Bondur
Mitiai (Dmytro) Bondur - avatar
+ 1
Thanks, it's turned out to be very simple - parentheses after Date.now :)
22nd Apr 2022, 7:04 PM
Mitiai (Dmytro) Bondur
Mitiai (Dmytro) Bondur - avatar