0

How can I move the rectangle

I wanted to move the rectangle relative with the button I click.But I don't have any clue why it's not working https://code.sololearn.com/W31LVGRx9usd/?ref=app

1st Sep 2017, 5:19 PM
Chris
Chris - avatar
3 Respostas
+ 5
Change line 42 - 51 to this: var button=document.getElementById ("buttony").addEventListener ("click" , function() { moveUp=setInterval(function (){ if(posY<0){ clearInterval(moveUp); return; } posY-=1; ctx.fillStyle="Blue"; ctx.fillRect (posX,posY,70,70); ctx.strokeStyle="white"; ctx.lineWidth=5; ctx.strokeRect (posX,posY,70,70); },30) }); //then figure out where you went wrong
1st Sep 2017, 5:26 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
You need functions for all buttons yes, but the code will slightly change for each button. -Take note how the "posY" gets decreased for the up button. -Take note how an if statement stops the square from moving up when the "posY" is less than 0. -Take note that I didn't include any code for "posX" for the up button. -Take note of all these changes I made in the code. You should be able to figure this out now.
1st Sep 2017, 5:56 PM
Ghauth Christians
Ghauth Christians - avatar
0
I appreciate it so much but do I have to duplicate the code for all buttons.
1st Sep 2017, 5:51 PM
Chris
Chris - avatar