# How do I Create automatic doors?

Get help using Construct 2

### » Sun Sep 28, 2014 9:11 pm

It is a sidescroller. The idea is simple: Player walks up to a door, door opens. once player walks away from the door, door closes again.

i do this with player.x and door.x but here is the thing: it should work from both sides, so i figure i have to use lerp but i have no idea how that works. also, the door must always open fuly and close fully, preferably until it hits a solid tile.

so the code should be something like
if door.x is less than player.x+80 then use 8 direction up until you hit a solid tile. then switch on instance variable to "open" and stay there. if player is away more than 80 then close until you hit a solid tile and then switch to instance variable "closed"

i can do most of that but i have a problem with the range. how exactly do i tell the door that it should only move if player is closer than 80 in both directions, left and right? and only that particular door?
B
7
S
2
Posts: 68
Reputation: 724

### » Sun Sep 28, 2014 9:17 pm

You can use distance(player.X, player.Y, door.X, door.Y)
B
161
S
69
G
44
Posts: 2,612
Reputation: 36,881

### » Sun Sep 28, 2014 9:24 pm

Okay, and how do i set it up? do i put that in the door sprite? compare X and then this or how does that work?
B
7
S
2
Posts: 68
Reputation: 724

### » Sun Sep 28, 2014 9:29 pm

Simply compare two values.
if distance(player.X, player.Y, door.X, door.Y) > 80 - close doors
else open doors
B
161
S
69
G
44
Posts: 2,612
Reputation: 36,881

### » Sun Sep 28, 2014 9:34 pm

okay but HOW EXACTLY do i set that up?Sorry but I have NO clue what that means
B
7
S
2
Posts: 68
Reputation: 724

### » Sun Sep 28, 2014 9:45 pm

compare two values is a system event.

You will compare the distance of the two objects as @shinkan described.

This will have an action to either keep the doors open, or close them depending on how you set it up. You will have to have a sprite for this, and probably disable collision on the door when open.

you then need an else statement to do exactly the opposite.
B
29
S
9
G
1
Posts: 226
Reputation: 2,990

### » Sun Sep 28, 2014 9:46 pm

Very rough example

If you don't have r182 here's the screenshot:
player_door.png
You do not have the required permissions to view the files attached to this post.
B
161
S
69
G
44
Posts: 2,612
Reputation: 36,881

### » Sun Sep 28, 2014 9:50 pm

okay thanks,
i set that up with else and everything but it still does not work
i put a soundfile in just to test before i ut the open door code in and, well, the soundfile plays over and over until i move close to the door on one side, and that's it.

i have multiple doors, is that the problem?

edit: i put in the movement and now all doors in the map just move down and are gone - no matter how close i am
B
7
S
2
Posts: 68
Reputation: 724

### » Sun Sep 28, 2014 10:03 pm

personally I find it easier having an open animation frame and a closed animation frame. All you do is change the frame. No movement or changing angles.
Just remember to turn off/on the collision.

This way keeps sprites where they need to be pretty reliably.
B
29
S
9
G
1
Posts: 226
Reputation: 2,990

### » Sun Sep 28, 2014 10:07 pm

still doesnt change the fact that i can't get it to work to actually do something when the player is closer than 80 on both X sides. i can get it to work on one X side but not on both. Technically i could try to fake it by mirroring it depending on where the player is and then check the door if it is mirrored or not and compare that to the player distance on X BUT that is an overly complicated way and does not really do what i need since the door would then only move if the player is within that radius.

so any help on that would be appreciated, but thanks for the comments so far
B
7
S
2
Posts: 68
Reputation: 724

Next