To make the character’s movement I made something like this:
local function onEnterFrame() -- "jugador" means player in spanish --"todoGroup" is the group containing all the elements if gamePad.isMoving then if gamePad.isMovingUp then jugador:translate(0, -1\* (movementSpeed \* deltaTime )) todoGroup:translate(0, (movementSpeed \* deltaTime )) jugador:setSequence("deAtrasCaminando") end if gamePad.isMovingDown then jugador:translate(0, (movementSpeed \* deltaTime )) todoGroup:translate(0, -1\* (movementSpeed \* deltaTime )) jugador:setSequence("deFrenteCaminando") end if gamePad.isMovingLeft then jugador:translate(-1\* (movementSpeed \* deltaTime ), 0) todoGroup:translate( (movementSpeed \* deltaTime ), 0) jugador:setSequence("deIzquierdaCaminando") end if gamePad.isMovingRight then jugador:translate((movementSpeed \* deltaTime ), 0) todoGroup:translate(-1\* (movementSpeed \* deltaTime ), 0) jugador:setSequence("deDerechaCaminando") end pasosPermitidos = true else jugador:play() end Runtime:addEventListener("enterFrame",onEnterFrame)
But when the character collided with any static object, the camera continued moving without the character
so I solved this with RayCasting:
local function onEnterFrame() local deltaTime = getDeltaTime() local choquesDer = physics.rayCast( jugador.x, jugador.y, jugador.x + jugador.contentWidth/2 + 5, jugador.y , "closest" ) local choquesIz = physics.rayCast( jugador.x, jugador.y, jugador.x - jugador.contentWidth/2 - 5, jugador.y , "closest" ) local choquesAba = physics.rayCast( jugador.x, jugador.y, jugador.x , jugador.y + jugador.contentHeight/2 + 5 , "closest" ) local choquesArri = physics.rayCast( jugador.x, jugador.y, jugador.x, jugador.y - jugador.contentHeight/2 - 5 , "closest" ) if ( choquesDer ) then if choquesDer[1].position.x \> jugador.x then gamePad.isMovingRight = false end end if ( choquesIz ) then if choquesIz[1].position.x \< jugador.x then gamePad.isMovingLeft = false print(choquesIz[1].object) end end if ( choquesAba ) then if choquesAba[1].position.y \> jugador.y then gamePad.isMovingDown = false end end if ( choquesArri ) then if choquesArri[1].position.y \< jugador.y then gamePad.isMovingUp = false end end if gamePad.isMoving then if gamePad.isMovingUp then jugador:translate(0, -1\* (movementSpeed \* deltaTime )) todoGroup:translate(0, (movementSpeed \* deltaTime )) jugador:setSequence("deAtrasCaminando") end if gamePad.isMovingDown then jugador:translate(0, (movementSpeed \* deltaTime )) todoGroup:translate(0, -1\* (movementSpeed \* deltaTime )) jugador:setSequence("deFrenteCaminando") end if gamePad.isMovingLeft then jugador:translate(-1\* (movementSpeed \* deltaTime ), 0) todoGroup:translate( (movementSpeed \* deltaTime ), 0) jugador:setSequence("deIzquierdaCaminando") end if gamePad.isMovingRight then jugador:translate((movementSpeed \* deltaTime ), 0) todoGroup:translate(-1\* (movementSpeed \* deltaTime ), 0) jugador:setSequence("deDerechaCaminando") end pasosPermitidos = true else --jugador:setSequence("deFrenteCaminando") pasosPermitidos = false end jugador:play() end Runtime:addEventListener("enterFrame",onEnterFrame)
And this worked, but I got a problem, and is that if the character collides with anything that is a physic body, it would stop, so I tried something like this:
if ( choquesDer ) then if choquesDer[1].object.name == "borderRight" then gamePad.isMovingRight = false end end
But it returned the parameter choquesDer[1].object.name = nil
So, is there any way to get the object’s parameters with raycasting?
or is there other more efficient way to make the character stop when collides with certain objects?
Thanks for reading, have a good afternoon