Corona sdk walls

I am creating a game with balls and when they drop from the screen some of them are usually off of the screen so can someone help me on how to change this ?

local function spawnBall() for i=1,numberBall do Ball = display.newCircle(100, 80, 10) physics.addBody(Ball, "dynamic", {density=.1}) Ball:setFillColor( 0.7 ) Ball:setStrokeColor( 0, 0, 5 ) Ball:applyLinearImpulse(-.05, .05, 0, 0) Ball.x = math.random(-10, 400); Ball.y = -40; transition.to( Ball, { time=math.random(2000,8000), x=math.random(-10,400) , y=600, onComplete=clearBall } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); --Adding touch event end end

like this?

local physics = require("physics") physics.start( ) physics.setGravity( 0, 0 ) local numberBall = 20 local scale0= ((display.actualContentWidth- display.contentWidth)\*.5)\*-1 local scale0Y= ((display.actualContentHeight- display.contentHeight)\*.5)\*-1 local function spawnBall() for i=1,numberBall do local Ball Ball = display.newCircle(100, 80, 10) physics.addBody(Ball, "dynamic", {density=.1}) Ball:setFillColor( 0.7 ) Ball:setStrokeColor( 0, 0, 5 ) Ball:applyLinearImpulse(-.05, .05, 0, 0) Ball.x = math.random(-10, 400); Ball.y = -40; transition.to( Ball, { time=math.random(2000,8000), x=math.random(scale0,display.actualContentWidth) , y=600 } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); --Adding touch event end end spawnBall()

That dosen’t help 

How do balls should behaviour?

I want them to fall down but sometimes either they go past the left wall or they spawn over the wall . I want them to fall down the screen where I can see them 

1.You probably should not be using transition.to , use :applyForce or :setLinearVelocity. Transition.to may move the physics body, but is not pick up by the physics engine. 

  1. How to my example not help, Are you use letterbox? Can you post you config.lua?
application = { content = { width = 768, height = 480, scale = "letterBox", fps = 30, --[[imageSuffix = { ["@2x"] = 2, }, --]] }, --[[-- Push notifications notification = { iphone = { types = { "badge", "sound", "alert", "newsstand" } } }, --]] }

Try this (work on Corona simulator Samsung Galaxy 3 720x1280, idea based on post https://forums.coronalabs.com/topic/32843-how-can-i-detect-if-a-kinematic-object-is-on-the-screen/ )

local physics = require( "physics" ) -- Default  physics.setGravity( 0,  9.8 )  local function spawnBall()     local ball = display.newCircle( 100, 80, 10 )     ball.name= "ball"     physics.addBody( ball, "dynamic", {density=0.1} )     ball:setFillColor( 0.7 ) end local function onGlobalCollision( event )     if ( event.phase == "ended" ) then        if ( event.object1.name == "ball" ) then     display.remove( event.object1 )     elseif ( event.object2.name == "ball" ) then     display.remove( event.object2 )     end     end end physics.start() local screen = display.newRect( display.contentWidth \* 0.5, display.contentHeight \* 0.5, display.contentWidth, display.contentHeight - 400 ) screen.name = "screen" physics.addBody( screen,  "static" )  screen.isSensor = true Runtime:addEventListener( "collision", onGlobalCollision ) timer.performWithDelay( 1000, spawnBall, 3 )

Read about it :slight_smile:

Physics Setup - https://docs.coronalabs.com/guide/physics/physicsSetup/index.html

Collision Handling  -  https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#handling

object.isSensor - https://docs.coronalabs.com/api/type/Body/isSensor.html

Spawn enemy - https://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/

Hope this help :slight_smile:

EDIT : I add “- 400” so you see that ball disappear.

local screen = display.newRect( display.contentWidth \* 0.5, display.contentHeight \* 0.5, display.contentWidth, display.contentHeight - 400 )

like this?

local physics = require("physics") physics.start( ) physics.setGravity( 0, 0 ) local numberBall = 20 local scale0= ((display.actualContentWidth- display.contentWidth)\*.5)\*-1 local scale0Y= ((display.actualContentHeight- display.contentHeight)\*.5)\*-1 local function spawnBall() for i=1,numberBall do local Ball Ball = display.newCircle(100, 80, 10) physics.addBody(Ball, "dynamic", {density=.1}) Ball:setFillColor( 0.7 ) Ball:setStrokeColor( 0, 0, 5 ) Ball:applyLinearImpulse(-.05, .05, 0, 0) Ball.x = math.random(-10, 400); Ball.y = -40; transition.to( Ball, { time=math.random(2000,8000), x=math.random(scale0,display.actualContentWidth) , y=600 } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); --Adding touch event end end spawnBall()

That dosen’t help 

How do balls should behaviour?

I want them to fall down but sometimes either they go past the left wall or they spawn over the wall . I want them to fall down the screen where I can see them 

1.You probably should not be using transition.to , use :applyForce or :setLinearVelocity. Transition.to may move the physics body, but is not pick up by the physics engine. 

  1. How to my example not help, Are you use letterbox? Can you post you config.lua?
application = { content = { width = 768, height = 480, scale = "letterBox", fps = 30, --[[imageSuffix = { ["@2x"] = 2, }, --]] }, --[[-- Push notifications notification = { iphone = { types = { "badge", "sound", "alert", "newsstand" } } }, --]] }

Try this (work on Corona simulator Samsung Galaxy 3 720x1280, idea based on post https://forums.coronalabs.com/topic/32843-how-can-i-detect-if-a-kinematic-object-is-on-the-screen/ )

local physics = require( "physics" ) -- Default  physics.setGravity( 0,  9.8 )  local function spawnBall()     local ball = display.newCircle( 100, 80, 10 )     ball.name= "ball"     physics.addBody( ball, "dynamic", {density=0.1} )     ball:setFillColor( 0.7 ) end local function onGlobalCollision( event )     if ( event.phase == "ended" ) then        if ( event.object1.name == "ball" ) then     display.remove( event.object1 )     elseif ( event.object2.name == "ball" ) then     display.remove( event.object2 )     end     end end physics.start() local screen = display.newRect( display.contentWidth \* 0.5, display.contentHeight \* 0.5, display.contentWidth, display.contentHeight - 400 ) screen.name = "screen" physics.addBody( screen,  "static" )  screen.isSensor = true Runtime:addEventListener( "collision", onGlobalCollision ) timer.performWithDelay( 1000, spawnBall, 3 )

Read about it :slight_smile:

Physics Setup - https://docs.coronalabs.com/guide/physics/physicsSetup/index.html

Collision Handling  -  https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#handling

object.isSensor - https://docs.coronalabs.com/api/type/Body/isSensor.html

Spawn enemy - https://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/

Hope this help :slight_smile:

EDIT : I add “- 400” so you see that ball disappear.

local screen = display.newRect( display.contentWidth \* 0.5, display.contentHeight \* 0.5, display.contentWidth, display.contentHeight - 400 )