How do I keep an object within window?

0 favourites
  • 14 posts
From the Asset Store
Get Ready for the New Wave of "Bottom Screen" Games!
  • Hi

    so i am making a drag and drop game for a mobile app, Made it so it supports multiple screen sizes. The problem is that for the smaller screen size the objects can be dragged off screen. I tried bound to layout, but obviously that only bounds to the layout, so it can still leave the screen.

    what can i do to limit the movement of the objects to stay within the window?

    thank you

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • What do you want to to happen to the sprite when it nears or touches the area just outside the layout?

    1. Stop / drop?

    or

    2. Reposition to a random spot within your layout?

    or

    3. Reposition to a specific location within the layout?

  • hi thanks for the reply, i guess drop on spot would be the best option. I shall give this a go!

    again thank you

  • Sprite is dragging

    sprite set x - clamp(sprite.X,ViewportLeft(0),viewportright(0))

    sprite set y -clamp(sprite.Y,ViewportTop(0),viewportBottom(0))

    If you also want the sprite to keep the whole sprite within the Viewport:

    Sprite is dragging

    sprite set x - clamp(sprite.X,ViewportLeft(0)+0.5*Sprite.width,viewportright(0)-Sprite.width)

    sprite set y -clamp(sprite.Y,ViewportTop(0)+0.5*Sprite.Height,viewportBottom(0)-Sprite.Height)

  • We use the expressions:

    CanvasToLayerY("layer-name",0,0) <-- Top of the screen

    CanvasToLayerY("layer-name",WindowWidth,WindowHeight) <-- Bottom

    CanvasToLayerX("layer-name",0,0) <-- Left

    CanvasToLayerX("layer-name",WindowWidth,WindowHeight) <-- Right

    This will reliably give you the edge coordinates of the screen in any "fullscreen mode." Just keep awareness of scaling of the layer involved. I typically use a 0,0 parallax layer such as "UI."

    Then just do what LS says above to keep it on screen.

  • We use the expressions:

    CanvasToLayerY("layer-name",0,0) <-- Top of the screen

    CanvasToLayerY("layer-name",WindowWidth,WindowHeight) <-- Bottom

    CanvasToLayerX("layer-name",0,0) <-- Left

    CanvasToLayerX("layer-name",WindowWidth,WindowHeight) <-- Right

    This will reliably give you the edge coordinates of the screen in any "fullscreen mode." Just keep awareness of scaling of the layer involved. I typically use a 0,0 parallax layer such as "UI."

    Then just do what LS says above to keep it on screen.

    Why not use the viewport expressions?

    They do exactly the same..

    Using the (0) for layer zero (1) for layer 1, you can reference the correct layer the sprite is on so it will consider the scaling and paralax..

  • thanks for the replies, littlestain's method works fine, I might just test each one to learn. Just have to apply it to like 100 Sprites -.-

    I don't want to have a smaller layout size, I have done it like this to support multiple device sizes.

  • thanks for the replies, littlestain's method works fine, I might just test each one to learn. Just have to apply it to like 100 Sprites -.-

    I don't want to have a smaller layout size, I have done it like this to support multiple device sizes.

    Apply it to 100 sprites?

    Why would you have to do this?

    You have 100 completely different sprites on your screen?

    If so I would recommend using a family..

    If all sprites have the same behaviours and/or looks, why not just use 1?

  • Why not use the viewport expressions?

    They do exactly the same..

    Using the (0) for layer zero (1) for layer 1, you can reference the correct layer the sprite is on so it will consider the scaling and paralax..

    I would if that actually worked, but when you use "Crop" those expressions are no longer reliable for highly scalable layers. The CanvasToLayer expressions haven't failed me yet...

  • >

    > Why not use the viewport expressions?

    >

    > They do exactly the same..

    >

    > Using the (0) for layer zero (1) for layer 1, you can reference the correct layer the sprite is on so it will consider the scaling and paralax..

    >

    I would if that actually worked, but when you use "Crop" those expressions are no longer reliable for highly scalable layers. The CanvasToLayer expressions haven't failed me yet...

    I use "crop" all the time and the viewport expressions haven't failed me yet..

    As long as I remember to use the right layer between the (), that is...

  • I'll test it out on our client later, but I had issues with stuff staying where it was supposed to earlier. I'll build a test client sometime and show (along with another far more meddlesome issue that I need to resolve at some point).

  • I don't have them on one layout but spread over many layouts, each stage uses a different amount of sprites and they fit in specific place so cannot use just one.

    I have behaviors and set the sprites as a family already, is it possible to add the clamp expression and add that to the family? would be cool if it can.

    If it didn't have to support multiple screen sizes i know bound to layout would fix my problem in a second.

  • I don't have them on one layout but spread over many layouts, each stage uses a different amount of sprites and they fit in specific place so cannot use just one.

    I have behaviors and set the sprites as a family already, is it possible to add the clamp expression and add that to the family? would be cool if it can.

    If it didn't have to support multiple screen sizes i know bound to layout would fix my problem in a second.

    I still think you could use less sprites, but yeah, you could add a:

    system every tick

    family set X position to clamp

    amily set Y position to clamp

    Should work, but I would try and cut down on the number of sprites if at all possible..

  • sweet, still pretty new to this only been using for a few days. i think i could if i really tried . But if i didn't would it really be that bad? each sprite is quite small, would it just make it a huge app or would there be any other effects? sorry if its a stupid question never programmed before

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