Windows [SOLVED] Turn light off when space bar is pressed

N

NoCodeMonkey

Guest
I'm trying to get a light to turn off when the player comes in collision with the light, presses the space bar, and the light turns off. This is the code i have but its still not working -_-

[obj_light]
CREATE
myLight = true;
pSpace = keyboard_check_pressed(vk_space);

STEP
if place_meeting(x,y,o_player) && (pSpace)
{
myLight = false;
}


if myLight = false
{
sprite_index = s_Light_out;
}
else if myLight = true
{
sprite_index = s_Light_on;
}
 
Check for the collision in the step event. You can then check for collision and keypress in just one if

Also, you could combine the lamp turned on and off in just one sprite (sub images), with the first one being the lamp turned off. If you create a boolean in create to keep track of the lamp status, you can use it to set the proper image index

STEP:
if (place_meeting(x, y, o_player) && keyboard_check_pressed(vk_space) {
lit = !lit;
image_index = lit;
}

CREATE:
lit = true;
image_speed = 0;
image_index = lit;
 
N

NoCodeMonkey

Guest
Check for the collision in the step event. You can then check for collision and keypress in just one if

Also, you could combine the lamp turned on and off in just one sprite (sub images), with the first one being the lamp turned off. If you create a boolean in create to keep track of the lamp status, you can use it to set the proper image index

STEP:
if (place_meeting(x, y, o_player) && keyboard_check_pressed(vk_space) {
lit = !lit;
image_index = lit;
}

CREATE:
lit = true;
image_speed = 0;
image_index = lit;
This is what i did using what you also showed me but still not working :/ i don't have anything else in the room but the player and the torch.
Code:
Create Event:

myFire = true;
image_speed = 0.5;
sprite_index = s_torch;

pSpace = keyboard_check_pressed(vk_space);

Step Event:

execute code:

if (place_meeting(x, y, o_player) && (pSpace)) {
myFire = !myFire;
sprite_index = s_torch_out;
}
 
I

Insanebrio

Guest
you have to put

pSpace = keyboard...

also at the start of your step event. With your current code, when the instance is created you are not pressing the key, so the variable is always false and your code always fails to run. Remember this for any key functions
 
You really need to check for keypress in the step event, not in create.
Creating this will do exactly what you want, nicely combining the lamp on and off in one sprite, and using a boolean to keep track:
 
N

NoCodeMonkey

Guest
i got it all to work now thanks :D also my lamp has a animation so thats why i have the lamp_off in its own sprite. Unless there's a GML command to select which images to cycle through?
 
That would require some code. Set the image speed to 0 and then cycle through the proper frames manually. Not all that difficult, but just keeping two seperate sprites is indeed easier in this case.
 
Top