How to make Sprites inside the screen will not go outside of the screen?

Hi guys, how to make Sprites inside the screen will not go outside of the screen? I have one character called xxx and if the xxx jumps with gravity, how to check whether the xxx will not be outside of the screen? Thanks guys.

Hi @jumoun,

You want to determine in advance whether a jump will take the character offscreen? Or during the jump? The first method would be very tricky as there are many factors/variables which could affect it. The second option is more realistic though…

Best regards,

Brent

During the jump, Brent. :slight_smile:

Hi @jumoun,

Some people may suggest doing a Runtime position check and determine the Y value of the character’s “base” and compare it to the Y value of the screen’s top edge, and then take action if the character passes beyond that point. That’s a valid way, BUT…

An easier way IMHO is to just use physics for this:

  1. Create a vector rectangle (display.newRect()) that covers the entire screen. Remember that this rectangle’s size may need to be adjusted in respect to your content area “scale”  setting. If this is a somewhat new concept to you, please let me know, since it’s very important you understand the concept of content area for this.

  2. Position the rectangle in the very center of the screen.

  3. Make this rectangle a physics body. Its type should be “kinematic” and it should be a sensor body.

  4. Set up collision filters (guide) so that the character and this rectangle interact only with each other.

  5. Set up collision detection and, in your collision listener function, look for the “event.phase” of “ended” for the character. This means that the character has technically exited the bounds of the rectangle, meaning he/she is offscreen. At that point, take whatever action you need to: for example, set its linear velocity to 0, or apply a force to send the character back downward. I’m not sure what you want to do when the character exits the screen bounds, but there are any number of possibilities.

  6. At some point, make the rectangle invisible (.isVisible = false) since you obviously don’t want to visually see this object. :slight_smile:

Hope that helps,

Brent

Hi @jumoun,

You want to determine in advance whether a jump will take the character offscreen? Or during the jump? The first method would be very tricky as there are many factors/variables which could affect it. The second option is more realistic though…

Best regards,

Brent

During the jump, Brent. :slight_smile:

Hi @jumoun,

Some people may suggest doing a Runtime position check and determine the Y value of the character’s “base” and compare it to the Y value of the screen’s top edge, and then take action if the character passes beyond that point. That’s a valid way, BUT…

An easier way IMHO is to just use physics for this:

  1. Create a vector rectangle (display.newRect()) that covers the entire screen. Remember that this rectangle’s size may need to be adjusted in respect to your content area “scale”  setting. If this is a somewhat new concept to you, please let me know, since it’s very important you understand the concept of content area for this.

  2. Position the rectangle in the very center of the screen.

  3. Make this rectangle a physics body. Its type should be “kinematic” and it should be a sensor body.

  4. Set up collision filters (guide) so that the character and this rectangle interact only with each other.

  5. Set up collision detection and, in your collision listener function, look for the “event.phase” of “ended” for the character. This means that the character has technically exited the bounds of the rectangle, meaning he/she is offscreen. At that point, take whatever action you need to: for example, set its linear velocity to 0, or apply a force to send the character back downward. I’m not sure what you want to do when the character exits the screen bounds, but there are any number of possibilities.

  6. At some point, make the rectangle invisible (.isVisible = false) since you obviously don’t want to visually see this object. :slight_smile:

Hope that helps,

Brent