+ 3
[SOLVED] movingBox
How to make the "Stop" button work? https://code.sololearn.com/Wrm1sxugCpsp/#
9 Antworten
+ 4
Just 2 corrections in js code,
In line 14
button1.onclick = stop;
In line 57
button.onclick = start;
Notes:
- when calling the functions from js part then they should nt be in double quotes
- since the functions "start" and "stop" are already defined, so to call them using event (onclick), we do not need the function call parentheses ( )
+ 5
evgeniy_pp, no probs, 😀
By adding these 2 lines inside stop function on top at line 48, we can reset the position to initial position.
box.style.left = "0px";
box.style.top = "0px";
+ 4
evgeniy_pp
Just copy line 2 to 5, on to the top of the code.
var pos = 0; //for moving right
var pos1 = 0; //for moving down
var pos2 = 490; //for moving left
var pos3 = 290; //for moving up
I leave the reasoning behind this to you, but the clue is "function scopes" and "global scopes"
+ 3
Tried to do some 'rework' on the code...
https://code.sololearn.com/W12hvL3uMTLo/?ref=app
+ 3
hint:
I'm learning Js just like you, so there is no warranty ;-)
+ 1
Morpheus, thank you. I tried to delete quotes but did not think about parentheses.
Actually, now I have a new problem XD
The box does not reset correctly if it's in right, bottom or left corners. Check the code, please.
I understand why it's happening but do not have a solution yet. I'm gonna to figure it out.
+ 1
Morpheus
Got it. Thank you again.
Just little curiosity: is it possible to make the box to return to starting position after pressing "Stop" using this code or I have to rewrite it completely to do this?
+ 1
Morpheus
I must already start to figure out things like that by myself 😅
0
KraBBy
I will look at it later, thank you. Good job, it really works now