+ 2

How do of find the coordinates of objects?

I am making a game with JS and I want a way by which I can get coordinates of my objects E.g. I have 2 rectangles .One is player and other is obstacle.I have made all animations of jump and go but I want to make a "You Lost" message when the 2 rectangles touch each other Please tell a way by which I can alert you lost when the two rectangles touch each other. Answers in JavaScript or CSS only .

21st Aug 2020, 6:40 AM
Raghu Rao
Raghu Rao - avatar
4 Réponses
+ 3
Get the current left and too property of rectangle using getComputedStyle and assign new value to rect.style.left=current left rect.style.top=current top and then keep increasing current left and top to move rectangle and check if their value is equal to other rectangle ,
21st Aug 2020, 7:52 AM
Abhay
Abhay - avatar
+ 2
Does rect.style.left=current left Contain the left side coordinates of rectangle
21st Aug 2020, 7:58 AM
Raghu Rao
Raghu Rao - avatar
+ 2
Raghu Rao yes but you need to assign the current css left value to current left variable ,and use getComputedStyle to get current css value or you can assign it directly as well to the variable
21st Aug 2020, 8:01 AM
Abhay
Abhay - avatar
+ 1
Abhay Thanks
21st Aug 2020, 8:02 AM
Raghu Rao
Raghu Rao - avatar