Log In  

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"
        if stat(32) == 16 and stat(33) == 0 and stat(34) == 1 then
         mode = "spr"
P#26924 2016-08-14 20:07 ( Edited 2016-08-15 03:35)

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

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

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)

P#26925 2016-08-14 23:35 ( Edited 2016-08-15 03:36)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-01-20 15:01 | 0.009s | 2097k | Q:11