Im having some trouble i cant figure out. Im trying to set up simple collision and it works but if they player continues to click into the collision radius they will be able to clip through. How can i stop this from happening? The code below has been edited so you can test it in simulator. I have already read over Robs post about this.
--Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Constants local centerXcord = display.contentCenterX local centerYcord = display.contentCenterY -----------------------------------------------Starts Map --Displays Map map = display.newRect(0,0,1024,1024 ) map:setFillColor(256,256,256) --Centers Map map.x = centerXcord map.y = centerYcord -- Variable used to move map along y axis motiony = 0; -- Variable used to move map along x axis motionx = 0; -- Walk Speed speed = 5; -- Move map function movemap (event) map.x = map.x + motionx; map.y = map.y + motiony; wall1.y = wall1.y + motiony; wall1.x = wall1.x + motionx; end Runtime:addEventListener("enterFrame", movemap) --Collision detection function hasCollided( animation, wall1 ) local left = animation.contentBounds.xMin \<= wall1.contentBounds.xMin and animation.contentBounds.xMax \>= wall1.contentBounds.xMin local right = animation.contentBounds.xMin \>= wall1.contentBounds.xMin and animation.contentBounds.xMin \<= wall1.contentBounds.xMax local up = animation.contentBounds.yMin \<= wall1.contentBounds.yMin and animation.contentBounds.yMax \>= wall1.contentBounds.yMin local down = animation.contentBounds.yMin \>= wall1.contentBounds.yMin and animation.contentBounds.yMin \<= wall1.contentBounds.yMax return (left or right) and (up or down) end --If collision has been detected then stop character movment function gameLoop( event ) if ( hasCollided( animation, wall1) ) then motionx = 0; motiony = 0; end return true end Runtime:addEventListener( "enterFrame", gameLoop ) wall1 = display.newRect(250, 250, 32, 32) --------------------------------------------Start Character animation = display.newRect( 0,0,32,32 ) animation.x = centerXcord animation.y = centerYcord ---------------------------------Start Controls -- Add left button left = display.newRect( 0,0,32,32 ) left.x = 15; left.y = 270; -- Add right button right = display.newRect( 0,0,32,32 ) right.x = 65; right.y = 270; -- Add up button up = display.newRect( 0,0,32,32 ) up.x = 40; up.y = 245; -- Add down button down = display.newRect( 0,0,32,32 ) down.x = 40; down.y = 295; -- When left arrow is touched, move character left function left:touch(event) --Targets the left button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; end return true end left:addEventListener("touch",left) -- When right arrow is touched, move character right function right:touch(event) --Targets the right button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; end return true end right:addEventListener("touch",right) -- When up arrow is touched, move character up function up:touch(event) --Targets the up button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; end return true end up:addEventListener("touch",up) -- When down arrow is touched, move character down function down:touch(event) --Targets the down button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; end return true end down:addEventListener("touch",down)