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

need help with one click change of sprites

so im trying to make a light turn on and off - but it always seems like im doing 1 - 1 or 1 / 2 cause i only know how to make like out match each other
here's the code:

GML:
if keyboard_check_pressed(vk_up) and sprite_index = scandle_on {
sprite_index = scandle_off;
}

if keyboard_check_pressed(vk_up) and sprite_index = scandle_off {
sprite_index = scandle_on;   
}
and i've tried with keyboard_check and keyboard_check_pressed
do anybody got anoter way of doing this without like?
making them not cancel out eachother?
 

Tobey

Member
You need to use else if like this:

GML:
if keyboard_check_pressed(vk_up) and sprite_index == scandle_on {
    sprite_index = scandle_off;
}

else if keyboard_check_pressed(vk_up) and sprite_index == scandle_off {
    sprite_index = scandle_on;   
}
This prevents the second statement from running if the first statement is true.
 

FrostyCat

Redemption Seeker
Read this: Why Yin-Yang if Blocks Suck
Many novices use Yin-Yang if Blocks with the hope that only one of the block bodies will execute on any given run. But when one block's body operates in a way that it enables some subsequent condition, this false hope becomes a big problem.

A particularly common example of such novice code is this:
Code:
if (variable == true) {
    variable = false;
}
if (variable == false) {
    variable = true;
}
A novice might think this flips variable between true and false. But a quick trace reveals a different conclusion:
  • variable starts out false: The first block doesn't run, second block makes it true. So far so good.
  • variable starts out true: The first block runs and makes it false. Then the second block runs because the first block made it false, and makes it true again.
There is a reason why asking what this particular piece does is a common weed-out question for junior-level technical interviews. Answering the novice way not only tells the employer you are green enough to have never seen or fallen for it before, it also tells the employer that you can't follow code the way a computer does. Neither of these are desirable coder qualities.
GML:
if (keyboard_check_pressed(vk_up)) {
    if (sprite_index == scandle_on) {
        sprite_index = scandle_off;
    } else {
        sprite_index = scandle_on;
    }
}
 
You need to use else if like this:

GML:
if keyboard_check_pressed(vk_up) and sprite_index == scandle_on {
    sprite_index = scandle_off;
}

else if keyboard_check_pressed(vk_up) and sprite_index == scandle_off {
    sprite_index = scandle_on;  
}
This prevents the second statement from running if the first statement is true.
thank you so much
 
Top