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

0 favourites
  • 7 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • 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!

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

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

    Obj.x = scroll.x - Obj.width/2
    Obj.y = scroll.y - Obj.height/2[/code:14tdufue]
  • 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..

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

    Where I have to put the things that you've written? I have used the action "Create object"

  • Try Construct 3

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

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

  • Thank you very much LittleStain! It's worked great!

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