GML Mouse click to change image [SOLVED]

Discussion in 'Programming' started by OnLashoc, Jul 12, 2019.

  1. OnLashoc

    OnLashoc Member

    Joined:
    Jan 15, 2018
    Posts:
    171
    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.
     
  2. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,450
    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 likes this.
  3. OnLashoc

    OnLashoc Member

    Joined:
    Jan 15, 2018
    Posts:
    171

    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!
     
    rIKmAN likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice