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

0 favourites
  • 5 posts
From the Asset Store
Vector illustration of a UFO with an alien in three different positions.
  • 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.

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

  • Do you want to keep changing the place? If not, you can use anchor behavior

  • Be carefull, it's

    (ViewportLeft("Layer") + ViewportRight("Layer")) / 2[/code:2k2l7if5]
    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:2k2l7if5]Object: set position to  (ViewportLeft(Self.LayerNumber) + ViewportRight(Self.LayerNumber)) / 2[/code:2k2l7if5]
    
    Also if you want something 10 pixels from the left of the screen, you just have to do [code:2k2l7if5]ViewportLeft(Self.LayerNumber) + 10[/code:2k2l7if5]
    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:2k2l7if5]ViewportLeft(Self.LayerNumber) + 10/LayerScale(Self.LayerNumber)[/code:2k2l7if5]
    
    Also, to be really complete, if your layer's scale rate isn't 100% you might need to do
    [code:2k2l7if5]ViewportLeft(Self.LayerNumber) + 10/(LayerScale(Self.LayerNumber) * LayerScaleRate(Self.LayerNumber))[/code:2k2l7if5]
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)