• 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 Change sprite of button only once

A

Agletsio

Guest
What I want to happen:

When I press the button, a red cross appears over it (image_index = 1). After this, the sprite should stay in that state as it has already been pushed. So basically the button can only be pressed once.

With my current code I, the sprite only stays in that state for one frame and then reverts back to image_index = 0. I assume this is because of my "else" argument becoming true in the following frame(because of keyboard_check_pressed). So I can see the problem but I have no idea hot to get around it and couldn't find anything on the web that provided clarity.

(the objects are created by a obj_room_control via instance_create. obj_dreams is the name of one of the buttons)

This seems like such a simple thing but having a lot of trouble with it. Any help or guidance will seriously be appreciated!

obj_dreams Create Event:
Code:
dream_pushed = false;

dream_text = false;

obj_dreams Step Event:

Code:
if place_meeting(x,y,obj_pointer) && dream_pushed = false {
dream_text = true;
}

else

dream_text = false;


if place_meeting(x,y,obj_pointer) && keyboard_check_pressed(vk_space) {
dream_pushed = true;
}

else

dream_pushed = false


if dream_pushed = true {
image_index = 1;
}


if dream_pushed = false {

image_index = 0;
}



obj_dreams Draw Event:

Code:
draw_self();

if dream_text = true {
draw_set_halign(fa_center)
draw_text(x,y+80,"Press'SPACE' to push button")
}
 
H

Homunculus

Guest
Why do you even need an else statement there? I mean, if the object never reverts back from a pushed state, just leave it be.

If you also want to shorten the code a bit, you could replace
Code:
if dream_pushed = true {
  image_index = 1;
}

if dream_pushed = false {
  image_index = 0;
}
with

Code:
image_index = dream_pushed;
 
A

Agletsio

Guest
So removed the "else" statement and now it works perfectly! Thanks for the guidance @Catan!

Strange thing is I'm sure I've done it before and then i got a compile error telling me that obj_dreams.dream_pushed not set before reading??

Also, how does "image_index = dream_pushed" know that I'm indicating frames? Is it because "false" and "true" are read as 0 and 1?

I apologize if these are stupid questions, I just want to make sure I understand how everything is working. Thanks for your help!
 
H

Homunculus

Guest
Not stupid questions at all, some languages don't allow that kind of boolean conversion. You are right though, true and false are, in fact, 1 and 0. This works for simple cases like yours.

obj_dreams.dream_pushed giving an error is probably related to other stuff, it generally happens when you try to access a variable of an object when there are no instances of that object
 
Top