How to wrap the screen around itself?

Hello. I have been trying to learn Corona for a little while now.

I’m trying to make a game where a player push a small ball back and forth. When the ball fall past the right or left edge of the screen I want it to reappear at the opposite edge.

I’ve been trying to find an event that can detect when a ball fall past the edge, but I haven’t found any. I think I could just put up an invisible object at the edge, set it to a sensor and have it detect it, but I was wondering if there was a specific event for it. Also I am unsure how you would render it. I don’t want to just change the position of the ball, rather when half the ball is past the right edge, the other half appears on the left.

Is there a good way to do this in Corona? I’ve been trying to look trough the documentation but I haven’t found anything.

Screen wrapping is something you have to implement on your own.

SSK2 provides ready made code to do this:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions2/#scene-wrapping-scene

My (SSK2) implemention uses:

  • A rectangle object to act as the wrapping bounds/area.  Usually hidden or alpha = 0.

    • Why?
      • Everyone knows how to make a rectangle object (newRect)
      • You can show it, color it, etc. if you need debug feedback during development.
      • Easy to manage.  Just put it in a group and when the group is removed so is the wrapRect.
      • Due to its nature, this means the wrapping rectangle size and placement can be arbitrary, thus making this technique easy and flexible.
  • The enterFrame listener

  • SSK2’s rectWrap function.

    – Create a rectangle to act as our ‘wrapping bounds’ local wrapProxy = display.newRect( display.contentCenterX, display.contentCenterY, 300, 300 ) – Show it a little so we can see where the wrap should happen wrapProxy.alpha = 0.1 – Place a player in the center of the wrapProxy and give it a random velocity local player = display.newCirle( wrapProxy.x, wrapProxy.y, 20 ) physics:addBody( player ) player:setLinearVelocity( math.random( -100, 100), math.random( -100, 100) ) – Start an enter frame listener and test for wrapping each frame function player.enterFrame( self ) ssk.actions.scene.rectWrap( self, wrapProxy ) end Runtime:addEventListener( “enterFrame”, player )

Note: SSK2 also has a circle wrapper for a circular wrapping region if that is an effect you’re looking for.

Huh, that’s interesting. Thank you.

So far what I’ve done is I’ve made a couple of lines at the edges of my screen. Originally I wanted to use them for collision detection, then teleport the object to the opposite side of the screen once it went past them, but as I understand it you can’t update an objects .x and .y properties inside a collision event (I think I have to use a timer or something), so instead I just hooked it up tot the normal physics engine and have my ball bounce off them. (Makes it a bit more convenient to try things out if the ball doesn’t immediately disappear of screen)

I’ll check out this SSK2 thing :slight_smile:

Screen wrapping is something you have to implement on your own.

SSK2 provides ready made code to do this:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions2/#scene-wrapping-scene

My (SSK2) implemention uses:

  • A rectangle object to act as the wrapping bounds/area.  Usually hidden or alpha = 0.

    • Why?
      • Everyone knows how to make a rectangle object (newRect)
      • You can show it, color it, etc. if you need debug feedback during development.
      • Easy to manage.  Just put it in a group and when the group is removed so is the wrapRect.
      • Due to its nature, this means the wrapping rectangle size and placement can be arbitrary, thus making this technique easy and flexible.
  • The enterFrame listener

  • SSK2’s rectWrap function.

    – Create a rectangle to act as our ‘wrapping bounds’ local wrapProxy = display.newRect( display.contentCenterX, display.contentCenterY, 300, 300 ) – Show it a little so we can see where the wrap should happen wrapProxy.alpha = 0.1 – Place a player in the center of the wrapProxy and give it a random velocity local player = display.newCirle( wrapProxy.x, wrapProxy.y, 20 ) physics:addBody( player ) player:setLinearVelocity( math.random( -100, 100), math.random( -100, 100) ) – Start an enter frame listener and test for wrapping each frame function player.enterFrame( self ) ssk.actions.scene.rectWrap( self, wrapProxy ) end Runtime:addEventListener( “enterFrame”, player )

Note: SSK2 also has a circle wrapper for a circular wrapping region if that is an effect you’re looking for.

Huh, that’s interesting. Thank you.

So far what I’ve done is I’ve made a couple of lines at the edges of my screen. Originally I wanted to use them for collision detection, then teleport the object to the opposite side of the screen once it went past them, but as I understand it you can’t update an objects .x and .y properties inside a collision event (I think I have to use a timer or something), so instead I just hooked it up tot the normal physics engine and have my ball bounce off them. (Makes it a bit more convenient to try things out if the ball doesn’t immediately disappear of screen)

I’ll check out this SSK2 thing :slight_smile: