How do I keep an object within window?

Get help using Construct 2

Post » Tue Jun 09, 2015 12:28 pm

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
B
5
Posts: 5
Reputation: 235

Post » Tue Jun 09, 2015 1:39 pm

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?
Last edited by producermark11 on Tue Jun 09, 2015 3:25 pm, edited 1 time in total.
B
35
S
10
G
4
Posts: 250
Reputation: 7,938

Post » Tue Jun 09, 2015 2:59 pm

hi thanks for the reply, i guess drop on spot would be the best option. I shall give this a go!
again thank you
B
5
Posts: 5
Reputation: 235

Post » Tue Jun 09, 2015 3:07 pm

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)
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 » Tue Jun 09, 2015 3:09 pm

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.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,415
Reputation: 4,824

Post » Tue Jun 09, 2015 3:13 pm

gumshoe2029 wrote: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..
Last edited by LittleStain on Tue Jun 09, 2015 3:17 pm, edited 1 time in total.
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 » Tue Jun 09, 2015 3:30 pm

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.
B
5
Posts: 5
Reputation: 235

Post » Tue Jun 09, 2015 3:47 pm

tonton128 wrote: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?
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 » Tue Jun 09, 2015 4:05 pm

LittleStain wrote: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...
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,415
Reputation: 4,824

Post » Tue Jun 09, 2015 4:12 pm

gumshoe2029 wrote:
LittleStain wrote: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 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

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests