How do I Set Position of Element Relative To Window/Layout?

Get help using Construct 2

Post » Tue Mar 11, 2014 12:59 pm

This seems like it should be such a simple problem to resolve, but I can't get my elements to sit X amount of pixels from the left of the window and X amount from the top of the window in my project, don't get me started on perfectly centring something.

I did find a line of code somewhere for centring something horizontally and vertically, but it doesn't seem to work. ViewportLeft("Layer") + ViewportRight("Layer") / 2 is meant to centre a sprite or object horizontally centre, but I find my element is either too far to the left or too far to the right. I am using scale mode, "Scale Outer" my project window size is 640 x 960 and the subsequent layouts are also the same size, each layout has unbounded scrolling set to yes.

I want to perfectly position an element centre, I want to position another element at the bottom of the screen which from my understanding should work using ViewportTop("Layer") + ViewportBottom("Layer") - offsetvalue, however I find my element is being positioned from the top down, not at the bottom of the screen. Am I doing something wrong here? Perhaps the scaling mode is breaking things.
B
6
S
1
Posts: 43
Reputation: 536

Post » Tue Mar 11, 2014 1:12 pm

Did you try with :

WindowWidth
WindowHeight
Get the size of the window. Useful with the Fullscreen in browser property of the project, since these enable a variable window size. See also Supporting multiple screen sizes.
B
39
S
15
G
5
Posts: 295
Reputation: 6,089

Post » Tue Mar 11, 2014 1:14 pm

Do you want to keep changing the place? If not, you can use anchor behavior
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Tue Mar 11, 2014 10:10 pm

Be carefull, it's
Code: Select all
(ViewportLeft("Layer") + ViewportRight("Layer")) / 2

With the parenthesis. You need to sum the values first and then divide by 2 (it's the average between the two positions)

I usually use something slightly more flexible:
Code: Select all
Object: set position to  (ViewportLeft(Self.LayerNumber) + ViewportRight(Self.LayerNumber)) / 2


Also if you want something 10 pixels from the left of the screen, you just have to do
Code: Select all
ViewportLeft(Self.LayerNumber) + 10

But if your layer is scaled, 10 pixels might appear greater or smaller than expected.

The way to counter layerscalling might be to do
Code: Select all
ViewportLeft(Self.LayerNumber) + 10/LayerScale(Self.LayerNumber)


Also, to be really complete, if your layer's scale rate isn't 100% you might need to do
Code: Select all
ViewportLeft(Self.LayerNumber) + 10/(LayerScale(Self.LayerNumber) * LayerScaleRate(Self.LayerNumber))
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Thu Mar 13, 2014 2:39 am

Yann,

You sir, are amazing. So glad I bought Construct 2 and joined this community, I'll give it a shot. My layers are set to scale 100%, so it shouldn't be a problem I presume to position things. I hope to be able to give back all of the help and some more when I get a firmer grasp on this tool.
B
6
S
1
Posts: 43
Reputation: 536


Return to How do I....?

Who is online

Users browsing this forum: dand, Google [Bot], mickclarke and 29 guests