Setting position to specific place on-screen

Get help using Construct 2

Post » Wed Dec 12, 2012 4:39 pm

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!
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Wed Dec 12, 2012 4:52 pm

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)Nimtrix2012-12-12 16:57:33
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Wed Dec 12, 2012 7:09 pm

@Nimitrix - 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.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Wed Dec 12, 2012 7:33 pm

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

justifun2012-12-12 19:33:55
B
50
S
21
G
8
Posts: 844
Reputation: 10,299

Post » Wed Dec 12, 2012 7:41 pm

@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.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Wed Dec 12, 2012 8:17 pm

@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?
B
50
S
21
G
8
Posts: 844
Reputation: 10,299

Post » Wed Dec 12, 2012 9:22 pm

@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.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Wed Dec 12, 2012 11:49 pm

You can use these expression:

ViewportLeft(layer)
ViewportRight(layer)
ViewportTop(layer)
ViewportBottom(layer)

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
etc...
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Dec 14, 2012 3:39 pm

Thanks @ramones! I also found this: http://www.scirra.com/forum/what-is-x_topic56698.html

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?
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Fri Dec 14, 2012 6:28 pm

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)
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Next

Return to How do I....?

Who is online

Users browsing this forum: SpartCrafter and 23 guests