How do I keep a "player" inside a sprite

0 favourites
  • 8 posts
From the Asset Store
94 Inside buildings views - Isometric view - PNG transparent - 2048x2048
  • 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

  • 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.

  • 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

  • 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.

  • 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.

  • 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..

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • Use container

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