• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Mouse click to change image [SOLVED]

OnLashoc

Member
Ok Im a little confused what is going on here...

I have an image representing the size of the game and when the mouse is over the image, I want to be able to click the image which will change the image representing a different size. It's pretty simple looking but for some reason when I use the following code, it just stays on small if I have more than 2 sizes. See below:

Code:
if mouse_check_button_pressed(mb_left) && gameSize == 0 {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 1;
        }        
}

if gameSize == 1 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 2;
        } 
}

if gameSize == 2 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 0;
        }        
}
So it starts on Small pre-selected. Move you mouse over the image and left click and it should change to Medium. If it's on Medium and you move your mouse over the new image that says Medium and you left click it should change to Large. If it's on Large and you move your mouse over the new image that says Large and you left click it should change to Small.

I'm sure there is a better way to write / accomplish this, but Im stuck on it not changing from Small. I've tried putting this in the Step event and then tried removing from Step event and adding a Left Pressed Event.
 

rIKmAN

Member
Ok Im a little confused what is going on here...

I have an image representing the size of the game and when the mouse is over the image, I want to be able to click the image which will change the image representing a different size. It's pretty simple looking but for some reason when I use the following code, it just stays on small if I have more than 2 sizes. See below:

Code:
if mouse_check_button_pressed(mb_left) && gameSize == 0 {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 1;
        }    
}

if gameSize == 1 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 2;
        }
}

if gameSize == 2 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 0;
        }    
}
So it starts on Small pre-selected. Move you mouse over the image and left click and it should change to Medium. If it's on Medium and you move your mouse over the new image that says Medium and you left click it should change to Large. If it's on Large and you move your mouse over the new image that says Large and you left click it should change to Small.

I'm sure there is a better way to write / accomplish this, but Im stuck on it not changing from Small. I've tried putting this in the Step event and then tried removing from Step event and adding a Left Pressed Event.
Your if statements are cascading into each other.

So in the first one you set gameSize = 1, then it checks the next one and now gameSize = 1 so it sets it to 2, then the next one gameSize is now 2 so it sets it to 0 etc

Use "else if" on the subsequent checks so it only evaluates one of the if conditions each step.

ie.
Code:
if mouse_check_button_pressed(mb_left) && gameSize == 0 {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 1;
        }    
}

else if gameSize == 1 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 2;
        }
}

else if gameSize == 2 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 0;
        }    
}
 

OnLashoc

Member
Your if statements are cascading into each other.

So in the first one you set gameSize = 1, then it checks the next one and now gameSize = 1 so it sets it to 2, then the next one gameSize is now 2 so it sets it to 0 etc

Use "else if" on the subsequent checks so it only evaluates one of the if conditions each step.

ie.
Code:
if mouse_check_button_pressed(mb_left) && gameSize == 0 {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 1;
        }  
}

else if gameSize == 1 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 2;
        }
}

else if gameSize == 2 && mouse_check_button_pressed(mb_left)  {
    if collision_point(mouse_x, mouse_y, oChoice1, true, false)
        {
            gameSize = 0;
        }  
}

Damnit rIK lol, I tried the else if but I fat fingered it and it didn't work. Didn't realize until I seen you had it on the last section of the code!

Thank you! Working like a charm now!
 
Top