[SOLVED] How do I check which value in array is active?

A

adval25

Guest
Sorry if the question was a little hard to understand. I'll try and break it down:

i've initialized an array cRect[ ] to hold the returned values of a collision check for various collision rectangles.

for (n = 0; n < arbitraryNumber; n++){
cRect[n] = collision_rectangle(32, 32 * n, 64, 64 * n, obj_myMouseCursor, false, true);
}

Okay, so suppose that 'arbitraryNumber' is equal to like 3 or whatever. There are going to be 3 collision boxes on the screen piled on top of one another.

What I want to know is, is there a way to check which value in the array is active when I hover my mouse over its respective collision box?

For example, when I hover my mouse over the rectangle collision area assigned to cRect[2], how can I tell gm to return that cRect[2] is active and not cRect[0] or cRect[1]. The number inside the brackets [ ] (aka the position in the array) is the really important piece of info that I want returned.
 
Last edited by a moderator:

jo-thijs

Member
Your collision boxes are kind of wierd.
Are they supposed to overlap and grow exponentially?
Because they do.

If you want every box to be the same size and to be contiguous to the previous box, then you should use this code instead:
Code:
for (n = 0; n < arbitraryNumber; n++){
cRect[n] = collision_rectangle(32, n * 32, 64, (n + 1) * 32, obj_myMouseCursor, false, true);
}
To check if the mouse is inside a rectangle, you can use point_in_rectangle(mouse_x, mouse_y, ...).

If your boxes are defined by the code I gave though, you can make it even easier:
Code:
if mouse_x >= 32 && mouse_x <= 32
&& mouse_y >= 0 && mouse_y < arbitraryNumber * 32 {
    // the mouse is hovering over cRect[mouse_y div 32]
}
 
A

adval25

Guest
Are they supposed to overlap and grow exponentially?
They're not supposed to overlap. They are supposed to be contiguous however. But you get the picture.

Anyway, 💩💩💩💩ing, great piece of code dude! Totally helped me out. I think you meant "mouse_x >= 32 && mouse_x <= 64" but no biggie.

It works like a charm thanks!
 
Top