# Keep object to the viewportright + player distance to edge?

Get help using Construct 2

### » Sat Jan 14, 2017 5:00 pm

I'm trying to keep something to the right of the viewport, but I also want it so that depending on how close the player (who is being scrolled with) is to the viewport, the extra distance is added or subtracted, say, from the centre of the viewport (the centre being where the player will usually be because of scrolling, except for level edges). Any idea how to calculate just the X distance between and add the difference if the player is to the right-of-centre of the viewport or subtract if to the left-of-centre?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Sat Jan 14, 2017 7:30 pm

couldn't you just set it to be half the view width + player.x at all times? or did I misunderstand something?
B
88
S
43
G
71
Posts: 601
Reputation: 43,669

### » Sat Jan 14, 2017 7:49 pm

spacedoubt wrote:couldn't you just set it to be half the view width + player.x at all times? or did I misunderstand something?

Yeah, but wouldn't the player.x be relative to the entire layout, rather than the position nearest the scrolling edge?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Sat Jan 14, 2017 7:54 pm

yes, but is that a problem?

I feel like I am missing something.. heh. Is the object's position relative to the player or the screen edge?
It sounds like you're saying it's relative to the player, and when the player is in their typical position (the center) that falls just to the right of the view? When the player goes left of center, does the object go left of the edge?
B
88
S
43
G
71
Posts: 601
Reputation: 43,669

### » Sat Jan 14, 2017 8:05 pm

spacedoubt wrote:yes, but is that a problem?

I feel like I am missing something.. heh. Is the object's position relative to the player or the screen edge?
It sounds like you're saying it's relative to the player, and when the player is in their typical position (the center) that falls just to the right of the view? When the player goes left of center, does the object go left of the edge?

The player is central to the screen when it's scrolls with the player, except for the edges of the layout, where the scroll stops and the player gets closer to the viewport sides (and top and bottom for that matter) so i need to offset the change with a value, where the centre of the screen is 0 and if the player is closer to the left it's a minus figure and to the right a +, same goes for top and bottom. But I don't know to measure the distance of an object from the viewport sides
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Sat Jan 14, 2017 8:29 pm

It still sounds kinda like the viewport side is not what you really need to be relative to..
If it's always the same distance from the player, I'd just attach it that way.

But you can use distance(viewportright(layer),0,object.x,0) to get just the x distance.
B
88
S
43
G
71
Posts: 601
Reputation: 43,669

### » Sat Jan 14, 2017 9:40 pm

really? you can null the y parts with a 0 just to retreive the x distance? I didn't know that!

Yeah, I'm trying to get when the player is central to the screen exactly, so in the middle of a level, far from the sides, with scroll attached, let's say thats 0. If the player gets near to the left and the screen stops scrolling to the left, I want the figure to become a negative until the player touches the edge, in which case it should theoretically be minus half the screen width. For the right, if the layout gets to the edge, every point closer the player walks would add positive until the player hits the side and it should be positive half the screen width.

Can I establish this distance as a range in this way?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Sat Jan 14, 2017 10:00 pm

yeah, I mean think about it.. as long as the y values are the same, it's just measuring a straight horizontal line.

Let me get this straight:
Player in center = object is right edge
Player is at left edge = object in center
Player at right edge = object half a screen beyond right edge

Is that right?
And you want to use the distance the player is from the edge to set where the object goes?
I worked on this for a minute, and I just keep looping back around to setting the object.x to player.x + half a screen
Is there some reason that's not an option?
B
88
S
43
G
71
Posts: 601
Reputation: 43,669

### » Sat Jan 14, 2017 10:22 pm

Actually, if have a different code set if the player is mirrored, so it would switch to being half again outside the left side of the screen. But thinking about what you're saying, I can't think of a reason that wouldn't work! I just had this idea it would require something more technical, but I think if this works I'll be more embarrassed than I've ever been embarrassed before! Let me see...
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286