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

Open/close door key_release

Soso

Member
[Create Event] obj_door
Door_state = 0;
door_id = 1 is assigned in room editor(using multiple instance of obj_door

[Step event]
If door_state = 0
{Sprite_index = spr_door_close;
solid = true; }

If door_state = 1
{Sprite_index = spr_door_open
solid = false; }

[Keyboard Event "Q"]
//open door
If distance_to_object (x,y,obj_player) <= 32 && door_id = 1 && door_state = 1
{door_state = 0;}

//close door
If distance_to_object (x,y,obj_player) <= 32 && door_id = 1 && door_state = 0
{door_state = 1;}

The door opens/close, but only in specific spots and i have a player2 obj that uses the ctrl key to do the same.

Am i using the distance to object right?
 
Last edited:

Tulloch

Member
The first thing I noticed right away is "Door_state" and "door_state". You should keep capitalization the same.

Your keyboard event is incorrect. It should be:

If distance_to_object (x,y,obj_player) <= 32 && door_id = 1 && door_state=1
{door_state = 0} else {door_state=1}
 
Last edited:

Tsa05

Member
It looks like you're setting the door_id equal to empty string in your door's create event, but comparing it against a number in your keypress event. This will cause a type mismatch error unless the door's id is changed to a number at some point. Maybe that's already happening, though, if you aren't getting an error message.
 

Soso

Member
The first thing I noticed right away is "Door_state" and "door_state". You should keep capitalization the same.

Your keyboard event is incorrect. It should be:

If distance_to_object (x,y,obj_player) <= 32 && door_id = 1 && door_state=1
{door_state = 0} else {door_state=1}
I forgot the code for closing the door
Just edit it in also whats wrong with my keyboard event?
 

Soso

Member
It looks like you're setting the door_id equal to empty string in your door's create event, but comparing it against a number in your keypress event. This will cause a type mismatch error unless the door's id is changed to a number at some point. Maybe that's already happening, though, if you aren't getting an error message.
Yeah sorry the door_id is assign in creation code of the room editor
My problem isnt that the doors dont work they just don't work well. If the player stand right next to the door it wont open only opens/closes in certain spots even tho it well within 32pixels of the door.
 

Tulloch

Member
Ah okay, my apologies. I guess I didn't understand the problem fully.
I would start by increasing the distance, perhaps to 64 or more to see if it's a problem with your code. If it seems to work alright, it is most likely a problem with your sprite. You can try centering the origin.
 

Soso

Member
Ah okay, my apologies. I guess I didn't understand the problem fully.
I would start by increasing the distance, perhaps to 64 or more to see if it's a problem with your code. If it seems to work alright, it is most likely a problem with your sprite. You can try centering the origin.
Yeah ive tried increasing the distance, ive tried using distance to point but no dice. Im not really sure what the problem is. Lol struggling so hard to code a simple door mechanic, but thanks for the reply
 

Tsa05

Member
Generally, the distance functions are going to measure between the origin points of each sprite, so you may need to plan accordingly. In the image below, you can see how a certain distance from a door's origin could lead to unusual hit detection. This can be fixed by centering the origin as Tulloch suggested, or by measuring to a point that you calculate. For example, center of the bottom of the door has the X coord of the door's x + 1/2 of the door's sprite width, and a Y coord of the door's y + the height of the door's sprite.



Edit: I should also mention that if the origin or position of your closed door sprite is different from that of your open door sprite, you'll experience an additional shift in detection when the door is open versus closed.
 
Last edited:
Top