Camera to follow rpg topdown character, but how?

Hi,

After spending some frustrating hours, I am resorting to asking in this forum for opinion of experts out there.

I have a map.json file and I am using Berrys Tiled engine to load it.

How to make sure the camera follows the character ?

  • The character when moves to the right, the camera should follow it

  • Similar behavior when character moves up or down

  • The map.x should move opposite to the direction of player.x so that there is a feeling that the camera is on the character

But how exactly to do it?

I tried :

local function movecharacter (event)

myAnimation.x = myAnimation.x + motionx;

myAnimation.y = myAnimation.y + motiony;

if (myAnimation.x > 300) then

  camera.x = myAnimation.x + display.contentCenterX

end

camera.y = myAnimation.y - display.contentCenterY

print (camera.x)

end

Runtime:addEventListener(“enterFrame”, movecharacter)

the map moves out of the left edge…and moves with the character…confused…how to implement this.

Nevermind solved it using, map was going the other way and also set a limit to what width the map should move :

if (myAnimation.x > 160 and myAnimation.x < visual.width-220) then

  camera.x = display.contentCenterX - myAnimation.x

end

if (myAnimation.y < 155) then

  camera.y = display.contentCenterY - myAnimation.y - 180

end

–print (myAnimation.y)

end

Runtime:addEventListener(“enterFrame”, movecharacter)

But noticed that the game was crashing…quite a lot when using runtime listener

Nevermind solved it using, map was going the other way and also set a limit to what width the map should move :

if (myAnimation.x > 160 and myAnimation.x < visual.width-220) then

  camera.x = display.contentCenterX - myAnimation.x

end

if (myAnimation.y < 155) then

  camera.y = display.contentCenterY - myAnimation.y - 180

end

–print (myAnimation.y)

end

Runtime:addEventListener(“enterFrame”, movecharacter)

But noticed that the game was crashing…quite a lot when using runtime listener