Why isn't not moving the variable square | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
16th Jun 2023, 12:01 PM
Leonard
2 Answers
+ 2
you should not use the id to style the square. You will end up with a bunch of duplicated ids. id should be unique. use class to style the square. also, after updating the x and y value, this.y += this.dy the style should also be updated to reflect the changes. This is what makes the element move. this.square.style.top = this.y+"px"; that's why you should maybe use this.square instead of var square to create element. also, try not to use var. it makes the variables global. use let instead. I played around with your code: https://code.sololearn.com/WzrcGtRUaVxd/?ref=app https://code.sololearn.com/WzX7fbKDsK37/?ref=app
16th Jun 2023, 6:49 PM
Bob_Li
Bob_Li - avatar
+ 4
Leonard Function "update" is incrementing the increment itself, instead of the actual square position: this.dy += this.y
16th Jun 2023, 12:45 PM
Emerson Prado
Emerson Prado - avatar