Changing colors upon mouse hover

M

mastmartelli

Guest
I've setup a system to draw 6 shapes on the screen (square, circle, triangle x2), and I have a script to randomly change their colors. The only problem I have is I can't seem to make it so if I hover my mouse over one of the shapes it'll run the change color script ONCE, instead it constantly runs the script over and over and the shapes flash random colors. Side note, I know the if statement in the for loop is probably a terrible way to draw them in order but I couldn't figure out another way.

Create event:
Code:
shapeNum = 6;
shapeOffset = 150;
shapeSize = 100;
shapeY = RESOLUTION_H/2 - shapeSize/2;

mouseInShape = false;
shapeColors = [c_red, c_orange, c_purple, c_aqua, c_yellow, c_green];
shapeCol = ds_list_create();
choose_shape_color();
Draw GUI event:
Code:
var pos = (RESOLUTION_W - (shapeOffset * 2) - shapeSize) / (shapeNum - 1);
var x1, x2, y1, y2, cX, cY, cLength, tX1, tX2, tX3, tY1, tY2, tY3;
var changedCol = false;
for (var i = 0; i < shapeNum; i++) {
    x1 = shapeOffset + (pos * i);
    x2 = x1 + shapeSize;
    y1 = shapeY;
    y2 = shapeY + shapeSize;
    cX = x2 - shapeSize / 2;
    cY = y2 - shapeSize / 2;
    cLength = shapeSize / 2;
    tX1 = x1 + shapeSize/2;
    tY1 = y1;
    tX2 = x1;
    tY2 = y2;
    tX3 = x2;
    tY3 = y2;
    
    draw_set_color(shapeCol[| i]);
    if (i == 0 || i == 3) draw_rectangle(x1, y1, x2, y2, false);
    else if (i == 1 || i == 4) draw_circle(cX, cY, cLength, false);
    else if (i == 2 || i == 5) draw_triangle(tX1, tY1, tX2, tY2, tX3, tY3, false);
    
    //this is one of many attempts to get the mouse hover to work and change the color only once, I'd prefer for it to work with all 3 shapes, I got so fed up I tried with only rectangles on this attempt
    if (!changedCol) {
        if (!mouseInShape && point_in_rectangle(mouse_x, mouse_y, x1, y1, x2, y2)) {
            choose_shape_color();
            changedCol = true;
            mouseInShape = true;
        }
        else {
            mouseInShape = false;
        }
    }
}
choose_shape_color script:
Code:
ds_list_clear(shapeCol);

var tempList = ds_list_create();
for (var i = 0; i < array_length_1d(shapeColors); i++) tempList[| i] = shapeColors[i];

var ranCol;
for (var i = 0; i < array_length_1d(shapeColors); i++) {
    ranCol = irandom_range(0, ds_list_size(tempList) - 1);
    
    shapeCol[| i] = tempList[| ranCol];
    ds_list_delete(tempList, ranCol);
}

ds_list_destroy(tempList);
 
S

Sarcasmonk

Guest
I had a similar issue when makign multicolored shields on my enemies

the way i fixed it was I had to put an if statement outside of my for loop that has variable if it = 1 then it goes in and then once it goes in
I have variable--; then an else if statement for you that would reset the variable back to 1 and also might need a boolean to check for if its being hovered.
so it might be something like this
if(changedCol = true && activatedVar = 1)
{
activatedVar--;
//ur code

}
else if
{
activatedVar = 1;
}

i hope that helps
 
Top