How do i keep objects from being able to move off screen?

so im making this game and i requires you to touch an object and drag it left and right but when i drag it left and right it is bale to move off of screen and then i cant get it back.

i thought a good solution would be to use an invisible wall placed on the sides of the screen where i wanted it to stop, so i pretty much used the platform image and modified its scale and then changed the alpha to 0. it works except when it touches the walls it bounces and glitches out kinda and doesnt look smooth at all so i figured there would be a better solution

is there a way to set the screen size as the parameters of which and object cant go out of?

i cant seem to be able to keep the object from going off screen here is my current code

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here display.setStatusBar( display.HiddenStatusBar ) local physics = require("physics") physics.start() physics.setGravity (0, 25) --Variable local background = display.newImageRect("background.png",320, 636 ) background.x = display.contentCenterX background.y = display.contentCenterY local platform = display.newImageRect("Platform.png", 320, 35) platform.x = display.contentCenterX platform.y = display.contentHeight - 2 platform.yScale = display.contentScaleY + 1 physics.addBody(platform, "static", {bounce = .2, friction = 0}) local leftWall = display.newImageRect("Platform.png", 320, 200) leftWall.x = display.contentWidth - 450 leftWall.y = display.contentHeight - 40 leftWall.alpha = 0 physics.addBody(leftWall, "static", {bouce = 0}) local rightWall = display.newImageRect("Platform.png", 320, 200) rightWall.x = display.contentWidth + 135 rightWall.y = display.contentHeight - 40 rightWall.alpha = 0 physics.addBody(rightWall, "static", {bounce = 0}) local ball = display.newImageRect("Ball.png", 50, 50) ball.x = display.contentCenterX ball.y = display.contentCenterY physics.addBody(ball, "dynamic", {bounce = 0}) --Functions local function onTouch(event) local ball = event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the ball display.currentStage:setFocus( ball ) -- Store initial offset position ball.touchOffsetX = event.x - ball.x elseif ( "moved" == phase ) then -- Move the ball to the new touch position ball.x = event.x - ball.touchOffsetX elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the ball display.currentStage:setFocus( nil ) end return true end ball:addEventListener("touch", onTouch)

In your “moved” phase, check to see if the x and y coordinates are within the viewable area.  If not, do not update the position.

Or use a physics dragger (touch joint) combined with your physics ‘walls’ idea. 

could you kinda show me how that would look in code please, im not familiar with alot of physics, and also was just wondering but is it possible to make it when i tilt my phone a certain way that it moves towards the side that im tilting it to?

There are sample projects available that display how to use touch joints. If you open your Simulator, click on Help and go to the Sample Projects option, you can scroll down to the “Physics” header and find the “Joints” option. Once there, you can click on File… and select the “show Project Files” option to find the actual code used.

oh thanks ill definitely do that right now

In your “moved” phase, check to see if the x and y coordinates are within the viewable area.  If not, do not update the position.

Or use a physics dragger (touch joint) combined with your physics ‘walls’ idea. 

could you kinda show me how that would look in code please, im not familiar with alot of physics, and also was just wondering but is it possible to make it when i tilt my phone a certain way that it moves towards the side that im tilting it to?

There are sample projects available that display how to use touch joints. If you open your Simulator, click on Help and go to the Sample Projects option, you can scroll down to the “Physics” header and find the “Joints” option. Once there, you can click on File… and select the “show Project Files” option to find the actual code used.

oh thanks ill definitely do that right now