Bound to layout....except....

Get help using Construct 2

Post » Sat Feb 22, 2014 5:17 am

Is there a way to 'bound to layout' except for say, the top, or left side, etc?

The only way I can think of, would require a lot of events... I'm trying to minimize by using "Bound to layout" but don't want them bound to the top of the screen... but for testing purposes, this can be any side.

Thanks!
B
47
S
15
G
3
Posts: 201
Reputation: 4,625

Post » Sat Feb 22, 2014 5:53 am

This should take 3 events to sort out. If you need the behavior to apply to many different objects then just put them in a group and apply these events to the group instead:

If Group.X < 0 then Group.X = 0
If Group.X > LayoutWidth then Group.X = LayoutWidth
If Group.Y > LayoutHeight then Group.Y = LayoutHeight

Of course you can tweak the zero or layout measurements for your sprite width or add a buffer if that suits the effect you're after. If you are using physics then it gets a little more complicated only in that you will need to set your physics velocity to zero to stop the object from repeatedly trying to exit the screen.
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Sat Feb 22, 2014 9:44 am

You could do it in one event with clamp ;
8 direction is moving : Set position to x: clamp(sprite.X,0,854) y :clamp(sprite.Y,-480,480);

This assumes that the layout width is 854, and height is 480 & would bound the player to layout except the top. Allowing the player to leave the layout up to 480 px upwards.

Edit : Don't forget to account for the sprite width and height ;
x : clamp(sprite.X,0+sprite.Width/2,854-sprite.width/2)
y : clamp(sprite.Y,-480+sprite.Width/2,480-sprite.width/2)
B
10
S
2
G
1
Posts: 202
Reputation: 1,545

Post » Sat Feb 22, 2014 9:09 pm

Noncentz705 wrote:You could do it in one event with clamp ;
8 direction is moving : Set position to x: clamp(sprite.X,0,854) y :clamp(sprite.Y,-480,480);

This assumes that the layout width is 854, and height is 480 & would bound the player to layout except the top. Allowing the player to leave the layout up to 480 px upwards.

Edit : Don't forget to account for the sprite width and height ;
x : clamp(sprite.X,0+sprite.Width/2,854-sprite.width/2)
y : clamp(sprite.Y,-480+sprite.Width/2,480-sprite.width/2)



Worked beautifully; thanks!
B
47
S
15
G
3
Posts: 201
Reputation: 4,625


Return to How do I....?

Who is online

Users browsing this forum: leandrogenske, megatronx and 10 guests