How do I keep a "player" inside a sprite

Get help using Construct 2

Post » Mon Oct 24, 2016 11:02 am

Hello,

I have got a player, wich is a drag and drop based sprite.
I need this player sprite to stay in a square sprite wich is under the player.
How can i 'stick' the player so he cant get dragged out of this area / sprite?

regards
Image

Coin Clicker 2 [Play store]
B
15
S
2
Posts: 75
Reputation: 905

Post » Mon Oct 24, 2016 11:11 am

You can use the overlapping (with the square sprite) and stop the player sprite (or push back the player sprite) when he leaves the overlapping.
B
12
S
7
G
4
Posts: 114
Reputation: 3,067

Post » Mon Oct 24, 2016 11:14 am

Pandadoor wrote:You can use the overlapping (with the square sprite) and stop the player sprite (or push back the player sprite) when he leaves the overlapping.


Thank you for your reply, if i use overlapping how exactly can i push back the player sprite or how can i hold him inside the square?

So at this moment i have: Player sprite - is overlapping -> Square
Image

Coin Clicker 2 [Play store]
B
15
S
2
Posts: 75
Reputation: 905

Post » Mon Oct 24, 2016 11:32 am

You can push back with speed (If you use the platform behavior there is speed)
Player Sprite -> isn't overlapping Square -> Player speed = -10

He won't be able to leave the square.
B
12
S
7
G
4
Posts: 114
Reputation: 3,067

Post » Mon Oct 24, 2016 1:24 pm

@RickUSBstick
You just need to clamp the Player's position to the Square's bounding box while dragging

Player Is dragging
Set X to clamp(Player.x, Square.BBoxLeft, Square.BBoxRight)
Set Y to clamp(Player.y, Square.BBoxTop, Square.BBoxBottom)

This will keep the Player's origin within the Square, so you might need to adjust the values to keep the entire image within the Square, e.g. add half the Player's image width to BBoxLeft and subtract it from BBoxRight.
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Mon Oct 24, 2016 2:25 pm

OddConfection wrote:@RickUSBstick
You just need to clamp the Player's position to the Square's bounding box while dragging

Player Is dragging
Set X to clamp(Player.x, Square.BBoxLeft, Square.BBoxRight)
Set Y to clamp(Player.y, Square.BBoxTop, Square.BBoxBottom)

This will keep the Player's origin within the Square, so you might need to adjust the values to keep the entire image within the Square, e.g. add half the Player's image width to BBoxLeft and subtract it from BBoxRight.


Thank you very much, i did it. :)
Now when i drag the player out of the square, the player stays inside the square (that's good), but when i drag my finger or mouse further i can still hit other things..
The player stays in the square but it stil hits things out the square like if the player is out of it when i drag my finger further.

Sorry for bad english.. :cry:

// What the player is doing is like everything is a kind of physics and the goal is to bounce other things away..
Image

Coin Clicker 2 [Play store]
B
15
S
2
Posts: 75
Reputation: 905

Post » Tue Oct 25, 2016 12:42 pm

RickUSBstick wrote:The player stays in the square but it stil hits things out the square like if the player is out of it when i drag my finger further.


If the collision checks are done before the position is reset, it will still collide at it's dragged position i.e under your finger. The solution would be to move the event that resets the position before any event that checks collision, but if the collision is handled by the physics behaviour rather than an event, then this won't work.

Another solution would be to Drop the Player as well as set it's when it goes out of the Square

For dragging past the left side do this:
Player.X < Square.BBoxLeft
Player Drop
Set Player.X to Square.BBoxLeft

And do something similar for the other sides
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731



Return to How do I....?

Who is online

Users browsing this forum: ivanski1983, KTML5, Yahoo [Bot] and 3 guests