Problem getting player to face correct direction

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

Line 19 if your code above says “elseif event.x<player”, whereas you probably meant to say “elseif event.x<playerChar.x”.

The reason for the error is that there is no variable named “player” (it’s nil), and you’re attempting to compare event.x (a number) to it.  That’s why the error referred to comparing a number to nil.

For future reference, when you get an error message like that, it’ll also mention the line number where the error occurred.  Just go to that line number in your code as your first step in debugging the issue.

  • Andrew

Yep, cheers Andy, I just noticed that myself and came here to mention that it was me being stupid.

Line 19 if your code above says “elseif event.x<player”, whereas you probably meant to say “elseif event.x<playerChar.x”.

The reason for the error is that there is no variable named “player” (it’s nil), and you’re attempting to compare event.x (a number) to it.  That’s why the error referred to comparing a number to nil.

For future reference, when you get an error message like that, it’ll also mention the line number where the error occurred.  Just go to that line number in your code as your first step in debugging the issue.

  • Andrew

Yep, cheers Andy, I just noticed that myself and came here to mention that it was me being stupid.