Hi, I posted a little while back, and had a reasonably quick response, so thought I’d try again, with my latest issue. I posted this on StackOverflow too, just in case.
I have a simple project at the moment, to demonstrate my issue.
I have 4 boundaries around a player_object -> newRect. The boundaries are static, the player_object is dynamic. The following code does apply a collision detection, and prevents the player_object from moving through the boundary, but, if the user then pressed the direction key again, in the direction of the boundary that the player_object isTouching, the player_object passes through the boundary object then the collision detection reasserts, and the player_object’s movement stops. I’ve had a look at preCollision and object properties, but I think there is something inherently wrong in my approach. Ideas?
local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) -- system.activate( "multitouch" ) -- Variables width = display.actualContentWidth height = display.actualContentHeight movementX = 0 movementY = 0 movementSpeed = 5 playerMoving = false wDown = false aDown = false sDown = false dDown = false -- / -- Map group\_map1 = display.newGroup() mapBoundary\_left = display.newRect(width \* 0.051, height \* 0.5, width \* 0.01, height) mapBoundary\_right = display.newRect(width \* 0.949, height \* 0.5, width \* 0.01, height) mapBoundary\_top = display.newRect(width \* 0.5, height \* 0.051, width, width \* 0.01) mapBoundary\_bottom = display.newRect(width \* 0.5, height \* 0.949, width, width \* 0.01) mapBoundary\_left:setFillColor(1,1,1) mapBoundary\_right:setFillColor(1,1,1) mapBoundary\_top:setFillColor(1,1,1) mapBoundary\_bottom:setFillColor(1,1,1) mapBoundary\_left.type = "leftboundary" mapBoundary\_right.type = "rightboundary" mapBoundary\_top.type = "topboundary" mapBoundary\_bottom.type = "bottomboundary" physics.addBody( mapBoundary\_left, "static", {friction=1, bounce=0} ) physics.addBody( mapBoundary\_right, "static", {friction=1, bounce=0} ) physics.addBody( mapBoundary\_top, "static", {friction=1, bounce=0} ) physics.addBody( mapBoundary\_bottom, "static", {friction=1, bounce=0} ) -- / -- Player player\_object = display.newRect(width \* 0.5,height \* 0.5,10,10) physics.addBody( player\_object, "dynamic", {friction=1, bounce=0} ) player\_object.type = "player" function func\_collision(self, event) if event.other.type == "leftboundary" then aDown = false elseif event.other.type == "rightboundary" then dDown = false elseif event.other.type == "topboundary" then wDown = false elseif event.other.type == "bottomboundary" then sDown = false end end player\_object.collision = func\_collision player\_object:addEventListener("collision") -- / function func\_movePlayer(event) if aDown == true and dDown == true then movementX = 0 elseif aDown == true and dDown == false then movementX = -movementSpeed elseif aDown == false and dDown == true then movementX = movementSpeed elseif aDown == false and dDown == false then movementX = 0 end if wDown == true and sDown == true then movementY = 0 elseif wDown == true and sDown == false then movementY = -movementSpeed elseif wDown == false and sDown == true then movementY = movementSpeed elseif wDown == false and sDown == false then movementY = 0 end player\_object.x = player\_object.x + movementX player\_object.y = player\_object.y + movementY player\_object.rotation = 0 end Runtime:addEventListener("enterFrame", func\_movePlayer) function func\_onKeyEvent( event ) local message = "Key '" .. event.keyName .. "' was pressed " .. event.phase -- print( message ) if event.keyName == "w" then if event.phase == "down" then wDown = true elseif event.phase == "up" then wDown = false end end if event.keyName == "a" then if event.phase == "down" then aDown = true elseif event.phase == "up" then aDown = false end end if event.keyName == "s" then if event.phase == "down" then sDown = true elseif event.phase == "up" then sDown = false end end if event.keyName == "d" then if event.phase == "down" then dDown = true elseif event.phase == "up" then dDown = false end end if event.keyName == "space" then -- Attack/Jump? end end Runtime:addEventListener( "key", func\_onKeyEvent )