Moving a sprite around a border

Discussion and feedback on Construct 2

Post » Sat Feb 28, 2015 4:05 pm

Hi,

I am a newbie here. Today I created one small layout that works like, when clicked on left side screen, the sprite will move left side. When tapped on right side, the sprite will move right side.

I have put demo here You can see event sheet here I have setup the code , "in touch" the sprite moves left side and right side. And at the same time, I have added sub event , that checks, whether the sprite crosses screen border. If it crosses, then the movement will not happen.

My question is how to stop the sprite exactly at 0 on left side and 480px on right side.

Image

(For some reason, it does not work perfect on mobile devices)
B
9
S
2
Posts: 59
Reputation: 763

Post » Sat Feb 28, 2015 4:11 pm

I guess there are several ways to do that!

You can use the Behaviour "Bound to Layout" to keep the sprite inside the layout at all time.

You can also check if the Sprite.X is smaller or bigger than and move it to 0 or 480.
Frosty Elk AB
www.frostyelk.se
Have no fear, We come in peace!
B
13
S
3
G
1
Posts: 106
Reputation: 1,169

Post » Sat Feb 28, 2015 4:57 pm

Thanks for pointing about "Bound to layout". I will check and update here,
B
9
S
2
Posts: 59
Reputation: 763

Post » Sat Feb 28, 2015 8:29 pm

You can also use clamp to restrict a value "x" between two other values "a" and "b". The expression would be:

clamp(x, a, b,)

In your case:

clamp(self.X , 0+ Sprite.Width/2, 480-Sprite.Width/2)

Check the attached example.
You do not have the required permissions to view the files attached to this post.
composer - multimedia artist
www.eli0s.com/en/
B
63
S
26
G
4
Posts: 1,145
Reputation: 9,517

Post » Sun Mar 01, 2015 5:37 am

@eli0s Thank you so much, this is one of the best solutions.
B
9
S
2
Posts: 59
Reputation: 763

Post » Sun Mar 01, 2015 2:12 pm

@vijayrajesh , you are welcome!
composer - multimedia artist
www.eli0s.com/en/
B
63
S
26
G
4
Posts: 1,145
Reputation: 9,517


Return to Construct 2 General

Who is online

Users browsing this forum: lasneo and 5 guests