You are not logged in.
Simple script for things like menus. You create an array with coordinates of rectangles; the script draws rectangles, then tells you which one the mouse is colliding with (last collision returned in multiple case).
Example:
draw_rectarray([0,0,100,50, 0,50,100,100, 0,100,100,150], 0)
Draws 3 filled rectangles.
If the mouse is within (0,0) to (100,50), the script returns 0
If the mouse is within (0,50) to (100,100), the script returns 1
If the mouse is within (0,100) to (100,150), the script returns 2
/// @arg {real} array
/// @arg {bool} outline
/// @returns The index of the rectangle currently intersected by mouse or -1
/// GMLscripts.com/license
/*
* Part of SLIDGE Engine
* Redistribution is permitted
* Submitting to gmlscripts
*/
/*
* Draws a rectangle for each set of coordinates in an array
* Returns an array containing the # of each box that the mouse hit
*/
var A = argument0;
var O = argument1;
var retval = -1;
for(var i=0;i<array_length_1d(A); i+=4){
draw_rectangle(A[i+0], A[i+1], A[i+2], A[i+3], O);
if(point_in_rectangle(mouse_x, mouse_y, A[i+0], A[i+1], A[i+2], A[i+3])){
retval[array_length_1d(retval)] = i div 4;
}
}
return retval;
Last edited by Tsa05 (2018-06-24 19:15:34)
Offline
That's a nice idea. But something weird is going on here:
retval[array_length_1d(retval)] = i div 4;
This seems to return an array/list of colliding rectangles, though I'm not sure if array_length_1d(-1)
is valid.
Should it not just be this?
retval = i div 4;
Abusing forum power since 1986.
Offline
That's a nice idea. But something weird is going on here:
retval[array_length_1d(retval)] = i div 4;
This seems to return an array/list of colliding rectangles, though I'm not sure if
array_length_1d(-1)
is valid.Should it not just be this?
retval = i div 4;
Good point! The code does work; array_length_1d of a non-array returns 0....but that's not what the script says it does!~~
Fixing description. Basically, the idea is that sometimes more than one overlapping rectangle is "hit"; this script actually returns a list of all "hit" boxes!
Last edited by Tsa05 (2018-06-24 19:14:35)
Offline