• 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!

SOLVED Getting a sprite to switch between three sub images

flyinian

Member
I am trying to get a sprite to switch through three sub images.

If the player doesn't have the requirements to activate the button, display sub image 2.
If the player has the requirements, Turn the sprite to sub image 1.
if the player has the requirements and clicks on the sprite/instance, turn the sub image to 0.
If the player clicks on the instance when its on sub image 0, turn the sub image back to 1.

It starts in sub image 2 (Not allowed) until I meet the requirement (Number >= 10). Once I meet the requirement and I click on the instance, it won't stay on sub image 0. I can see it blink to sub image 0 but reverts back to sub image 1.

Create Event:
GML:
Number = 0;
_Active = false;
_Allow = false;




Step Event:
GML:
if (Number <= 10)
{
    _Allow = false;   
    _SpriteSubImage = 2;

};
else
{


    _Allow = true;   
    _SpriteSubImage = 1;


};


if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, id) && _Allow = true)
{

    if (_Active)
    {
        _Active = false;
        _SpriteSubImage = 1;           
        show_debug_message("Deactivated");
        
    }
    else
    {
        _Active = true;
        _SpriteSubImage = 0;       
        show_debug_message("Activated");
        
    }
    
};
Thank you.
 

FoufaDjo

Member
use draw_sprite_ext and set sub(arg 2) tp image_index and just change the image_index value in your code,but u have to put the sprite speed to 0 in the image editor
in ur case put _spritesubimage instead of image_index in the draw event.
Ex :
step event:
if hp > 0 {
image_index = 1; //alive
} else {
image_index = 2; //dead
}
draw event:
draw_sprite_ext(splayer, >>"here" image_index "here"<< ,x,y,1,1,0,c_white,1);
 
Last edited:

Nidoking

Member
The problem here, ultimately, is that your first check either sets the image_index to 1 or 2. There's no option for 0. So if you want it to stay at 0, you'll have to fix your logic.
 

flyinian

Member
Okay, After reading everyone's replies and doing some more testing I managed to get it to work. Below is the new code, I have a feeling its pretty bad but, it does what I want it to. If anyone would like to help with editing it, please feel free. Thanks everyone for the help.

Step Event:
GML:
if (_Number >= 10)
{
_Allow = true;
}
else
{
_Allow = false;   
};


if (_Allow = false)
{
_SpriteSubImage = 2;
};

if (_Allow = true)
{
    _SpriteSubImage = 1;
};

if (_Allow = true && _Active = true)
{
_SpriteSubImage = 0;
};

if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, id) && _Allow = true)
{
    if (_Active = true && _Allow = true)
    {
        _Active = false;
        _SpriteSubImage = 1;           
        show_debug_message("Deactivated");
        
    }
    else
    {
        _Active = true;
        _SpriteSubImage = 0;       
        show_debug_message("WORKS");
        
    }
    
};
 

Nidoking

Member
That's a whole lot of redundant logic there. You probably don't need to keep resetting the sprite subimage every step. If you just change it when you need to, that might simplify things. Also, you don't typically need to check whether something is true inside another check for the same condition.
 

flyinian

Member
That's a whole lot of redundant logic there. You probably don't need to keep resetting the sprite subimage every step. If you just change it when you need to, that might simplify things. Also, you don't typically need to check whether something is true inside another check for the same condition.

Thanks for the feedback.
At my current skill level I am unsure on how to proceed with changing the code. so, I'll be leaving it as is and possibly coming back to it in the future when my skill level has improved.
 

FrostyCat

Redemption Seeker
Come on, it's easy to optimize the code you got if you would just learn some basic book lines on if statements and conditionals in general.

The first bad pattern is comparisons to true and false, which is one of the most obvious marks of the uninitiated.
GML:
if (_Active = true && _Allow = true)
GML:
if (anotherVariable == false)
The book line is to just use that expression as-is for the true case, and use the ! operator for the false case.
GML:
if (_Active && _Allow)
GML:
if (!anotherVariable)
The second is this kind of structure with repeated conditions:
GML:
if (_Allow = false)
{
_SpriteSubImage = 2;
};

if (_Allow = true)
{
    _SpriteSubImage = 1;
};

if (_Allow = true && _Active = true)
{
_SpriteSubImage = 0;
};
The book line is to use nesting to de-duplicate repeated conditions, and else to check opposite cases instead of manually inverted conditions.
GML:
if (_Allow)
{
    if (_Active)
    {
        _SpriteSubImage = 0;
    }
    else
    {
        _SpriteSubImage = 1;
    }
}
else
{
    _SpriteSubImage = 2;
}
The third is this kind of "if true var=true, else var=false" code:
GML:
if (_Number >= 10)
{
    _Allow = true;
}
else
{
    _Allow = false;  
};
Since _Number >= 10 itself returns a Boolean value, the book line is to use that out of the box.
GML:
_Allow = _Number >= 10;
 

flyinian

Member
Come on, it's easy to optimize the code you got if you would just learn some basic book lines on if statements and conditionals in general.

The first bad pattern is comparisons to true and false, which is one of the most obvious marks of the uninitiated.
GML:
if (_Active = true && _Allow = true)
GML:
if (anotherVariable == false)
The book line is to just use that expression as-is for the true case, and use the ! operator for the false case.
GML:
if (_Active && _Allow)
GML:
if (!anotherVariable)
The second is this kind of structure with repeated conditions:
GML:
if (_Allow = false)
{
_SpriteSubImage = 2;
};

if (_Allow = true)
{
    _SpriteSubImage = 1;
};

if (_Allow = true && _Active = true)
{
_SpriteSubImage = 0;
};
The book line is to use nesting to de-duplicate repeated conditions, and else to check opposite cases instead of manually inverted conditions.
GML:
if (_Allow)
{
    if (_Active)
    {
        _SpriteSubImage = 0;
    }
    else
    {
        _SpriteSubImage = 1;
    }
}
else
{
    _SpriteSubImage = 2;
}
The third is this kind of "if true var=true, else var=false" code:
GML:
if (_Number >= 10)
{
    _Allow = true;
}
else
{
    _Allow = false; 
};
Since _Number >= 10 itself returns a Boolean value, the book line is to use that out of the box.
GML:
_Allow = _Number >= 10;
Thank you, this clarifies a few things for me.
 
Top