Setting position to specific place on-screen

0 favourites
  • 11 posts
From the Asset Store
Forgotten Place - Melodic ambiance, background music, Synth
  • Hello,

    I am just wondering the easiest way to position an object to, say, the centre of the screen after you have scrolled to somewhere. My layout is 640x480 and I want to position objects to certain places when created.

    So for example, create a sprite in the middle of the screen regardless of where that may be on the layout.

    I have come up with a number of solutions, such as having one sprite in a parralax 0,0 layer and spawning in relation to that, but they have all been really messy.

    Thanks for any suggestions!

  • You could just place it at ScrollX, ScrollY. That's the X and Y position of the camera, so that would be in the middle of the screen.

    Here's a quick example along with a few other uses of ScrollX and ScrollY:

    SpawnCenter.capx (r112)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • - thanks! That helps, but only part of the way - I have objects that I want to spawn in all different locations. For example top of the screen +200.

    Using your method and knowing my resolution enables me to place things fairly well, but then when screen sizes change it won't work (the screen size is bigger on iPad than iPhone for example. So if I want to spawn something from the top-left corner on both devices, I can't designate co-ordinates in reference to the screen centre. I would need the left and top sides).

    One method I used before was to make a sprite for each side of the screen, and on start of layout go:

    While 'screen top' is on-screen > set 'screen top' Y to 'self.Y-1'

    And do that for each side. It works well enough, however I have concerns about performance by having the extra four objects as it's already scraping in below 30 frames.

  • Use a percentage of the layout.width and layout.height instead of hard numbers

    that way, no matter what resolution, your object i will always spawn at 50% or whatever of the screen.

    for example.

    spawn at x.layout.width/2 will spawn your object at the center of the top of the screen no matter what resolution

  • justifun - That's a good idea, but the layout is much bigger than the window. So if I have scrolled say 400 pixels to the right, your method will spawn the object 400 pixels short as it only references the layout and not the window.

  • Tobye I'm confused. Do you want it to spawn somewhere between the maximum layout width? or just what's visible on the screen wherever you happen to be scrolling at the time?

  • justifun - I want it to spawn at a certain area on-screen, no matter where the screen is, not a certain point on layout. So if I use layout layout.width/2, and the width of the layout is 1280, it will always spawn at 640. So if my screen size is 480 wide, and I am scrolled all the way to the end of the layout, the position of 640 won't even be on the screen. I hope that makes sense, I am really tired sorry :(

    I realised just then that I can use my previous method of bushing borders to the edge of the screen and referencing from them, then destroy them after they have been referenced. That will take care of any potential performance issues, but still I would like the eventing to be cleaner.

  • You can use these expression:





    ViewportLeft(0) = the left edge of the screen (layer 0)

    Screen width = ViewportRight(0) - ViewportLeft(0)

    Centre X = ViewportLeft(0) + (ViewportRight(0) - ViewportLeft(0)) / 2

    Centre Y = ViewportTop(0) + (ViewportBottom(0) - ViewportTop(0)) / 2

    200 px from the top of the screen = ViewportTop(0) + 200


  • Thanks ramones! I also found this:

    It talks about 'absolute' (or canvas) being the pixel co-ordinates of the screen, which is very useful as well, but I can't quite figure out how to use it. In the expressions it has 'CanvasToLayer' and 'LayerToCanvas', but they aren't too clear on how they are used. Don't suppose you know?

  • Yeah you could use CanvasToLayer. If your canvas size is 640x480 and you want to spawn an object in the middle at 320,240 then you need to convert that canvas position to a layer position. So the position on layer 0 would be:

    CanvasToLayerX(0, 320, 240), CanvasToLayerY(0, 320, 240)

    or in general:

    CanvasToLayerX(0, WindowWidth/2, WindowHeight/2),

    CanvasToLayerY(0, WindowWidth/2, WindowHeight/2)

  • ramones - great, thanks!

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