I'm playing with the newly-found mouse features and I want to figure out if my mouse sprite is in range of a button sprite (as in if both sprites intersect)
Here's my code so far (checks for exact positioning but I don't know how to check if they intersect, not if they're in the same position)
--mouse support poke(0x5f2d, 1) mode = "none" function _update() if stat(32) == 8 and stat(33) == 0 and stat(34) == 1 then mode = "none" end if stat(32) == 16 and stat(33) == 0 and stat(34) == 1 then mode = "spr" end end
you're so close! You just need a little more logic.
(For future reference, the word to search for is collision.)
if you had an 8 pixel button, you could do
if (mousex>buttonx-1) and (mousex< buttonx+9) and (mousey>buttony-1) and (mousey<buttony+9) then --the cursor is over the button end
What's that? Too many conditions? An alternate method would be checking how far the cursor is from the middle of the button.
if (abs(mousex-buttonx+4)<4) and (abs(mousey-buttony+4)<4) then --over button end
One could also do a straight-line distance from middle to middle, but then you'd miss corners. (works great for circles, though)
As usual, I'll also point you to Jelpi. If you haven't already, installing the demo carts can be educational and fun. (see the manual near the beginning)
[Please log in to post a comment]