Can you help me with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

Can you help me with this code?

The circle that I made doesn't move from it's actual position which I want to but actually extends on the right side of screen I tried running it on my PC and it ran without any error! So I want help for that! https://code.sololearn.com/WT817VZk5cdW/?ref=app

26th Apr 2019, 4:59 PM
Harsh Tyagi
Harsh Tyagi - avatar
11 Answers
+ 65
27th Apr 2019, 8:59 AM
Jaseem Akhtar
+ 56
27th Apr 2019, 8:49 AM
Jaseem Akhtar
+ 7
Harsh Tyagi Is this code the exact same code your ran on your pc? And when say pc were you using Sololearn Website playground or some sort of text editor/ ide? As for the second question, when movement is equal to or greater than 375 the value on movement becomes negative thus putting the circle to the left off-screen.
27th Apr 2019, 1:07 AM
ODLNT
ODLNT - avatar
+ 7
Harsh Tyagi Well if that is the case then it appears that I can not help you. But maybe JaseemAkhtar can.
27th Apr 2019, 1:57 AM
ODLNT
ODLNT - avatar
+ 6
You need to redraw you the context image before you move the circle for every frame to get the amination effect. https://code.sololearn.com/Wk6OxeoVN2SH/#js
26th Apr 2019, 10:53 PM
ODLNT
ODLNT - avatar
+ 3
I don’t know wheather this is corect but this is working: var canvas; var canvasContext; var FPS = 60; var movement = 5; window.onload = function draw(){ canvas = document.getElementById('game_canvas'); canvasContext = canvas.getContext('2d'); canvasContext.clearRect(0, 0, canvas.width, canvas.height); canvasContext.fillStyle = "black"; canvasContext.fillRect(0,0,canvas.width,canvas.height); canvasContext.fillStyle = "white"; canvasContext.fillRect(10,100,10,200); canvasContext.fillStyle = "white"; canvasContext.fillRect(330,100,10,200); move(); setInterval(draw, 1000/FPS); } function move(){ movement++; canvasContext.fillStyle = "white"; canvasContext.beginPath(); canvasContext.arc(movement,150,5,0,Math.PI*2,true); canvasContext.fill(); if (movement >= 375) { movement = - movement; } } Im clearing the canvas now
26th Apr 2019, 9:08 PM
CloudWay
CloudWay - avatar
+ 3
ODLNT Thanks! But I didn't call draw() function in move() function to redraw on my PC but still it worked the same. Can you help me with it? And also why it is not moving backwards when it reaches position 375 i.e. near the white rect on right side of screen!
27th Apr 2019, 12:18 AM
Harsh Tyagi
Harsh Tyagi - avatar
+ 2
ODLNT YES I wrote the exact same code on my PC AND I use Sublime Text 3, notepad and notepad++ and I worked the same using all three of them
27th Apr 2019, 1:33 AM
Harsh Tyagi
Harsh Tyagi - avatar
+ 2
JaseemAkhtar Can you help me with this?
27th Apr 2019, 8:29 AM
Harsh Tyagi
Harsh Tyagi - avatar
+ 2
JaseemAkhtar I figured that out but I want the ball to have a to and fro motion when it strikes those two white rects on both sides
27th Apr 2019, 8:51 AM
Harsh Tyagi
Harsh Tyagi - avatar
0
How can use keyboard key in Java script
3rd May 2019, 5:55 PM
Mansour Sherzad
Mansour Sherzad - avatar