I have a player character who follows the users finger left and right across the bottom of the screen. What I would like to do is for him to flip to face the correct way when the user changes direction. This is the code I have…
[lua]
playerChar = display.newImageRect( gameobjects, “p1_asp.png”, 148, 379 );
playerChar.x = 1006; playerChar.y = 1300
playerChar.myName=“asp”
playerChar.canmove=false
playerChar.dir=“right” – Player starts facing to the right
local function movePlayer(event)
– Check for movement and reposition player
if(event.phase == ‘began’) then
if event.x>= playerChar.x-(playerChar.width/2) and event.x<=playerChar.x+(playerChar.width/2) then
playerChar.canmove=true
end
elseif(event.phase == ‘moved’) and playerChar.canmove==true then
if event.x>playerChar.x and playerChar.dir==“left” then
playerChar.xScale=-1
playerChar.dir=“right” – This is the line that I am getting the error on.
elseif event.x<player and playerChar.dir==“right” then
playerChar.xScale=-1
playerChar.dir=“left”
end
playerChar.x = event.x
elseif event.phase ==‘ended’ then
playerChar.canmove=false
end
--Check to stop player leaving screen
if playerChar.x < (0+playerChar.width/2) then
playerChar.x = (0+playerChar.width/2)
elseif playerChar.x > (_w-playerChar.width/2) then
playerChar.x = (_w-playerChar.width/2)
end
end
toucher=display.getCurrentStage()
toucher:addEventListener(“touch”, movePlayer)
[/lua]
The screen loads but as soon as I try to move the character I get the error “attempt to compare number with nil”. This is driving me mad, so can anyone shed some light on what I’m doing wrong please?
Many thanks
Chris