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)