How do I create an object at center of the window?

Get help using Construct 2

Post » Fri Sep 19, 2014 12:40 pm

Hi there! My intention is to spawn an object at center of the display showed by the window. How can I do? Thanks for your time!
B
17
S
3
Posts: 62
Reputation: 1,054

Post » Fri Sep 19, 2014 12:59 pm

I see that you're new here, Welcome!

i suggest before you post a topic on How do i.. you search the forum first.

and about your question, try this:

On "S" pressed > System Create object at (LayoutWidth/2, LayoutHeight/2) meaning that you'll have to divide them by two.

Good luck!
B
22
S
6
Posts: 244
Reputation: 2,700

Post » Fri Sep 19, 2014 2:30 pm

Get the current layout Scroll X and Scroll Y - those are always exactly in the center of the project, then position your object At the same position as scroll.x and scroll.y

If your X and Y position of the object are counted from it's upper left corner, then you will have to add offset to the object position with half of it's width and half of it's height.

In pseudo code it will be:
Code: Select all
Obj.x = scroll.x - Obj.width/2
Obj.y = scroll.y - Obj.height/2
B
4
S
1
Posts: 20
Reputation: 392

Post » Fri Sep 19, 2014 3:18 pm

And to make it even more complicated

if you want exactly the center of the screen/window/viewport of the app, use viewportleft(x)+0.5*(viewportright(x)-viewportleft(x)) and viewporttop(x)+0.5*(viewportbottom(x)-viewporttop(x)) where x is the layer you'd like to spawn your object..
If you are using fullscreenmodes this is probably the only way you'll get the right position..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sat Sep 20, 2014 11:47 am

Thanks all for the replies but they don't work.

@LitteStain Where I have to put the things that you've written? I have used the action "Create object"
B
17
S
3
Posts: 62
Reputation: 1,054

Post » Sat Sep 20, 2014 12:01 pm

Those are the x and the y coordinate.

x = viewportleft(x)+0.5*(viewportright(x)-viewportleft(x))
y = viewporttop(x)+0.5*(viewportbottom(x)-viewporttop(x))
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sat Sep 27, 2014 7:53 pm

Thank you very much @LittleStain! It's worked great!
B
17
S
3
Posts: 62
Reputation: 1,054


Return to How do I....?

Who is online

Users browsing this forum: Cubeeo, YoHoho and 17 guests