Page 1 of 2

I need help. ;(

Posted: Fri Aug 24, 2012 3:30 pm
by Juryo
I am making a game called "Insomnia". It's a 2d horror game. I'm having a bit trouble with player movement, I have the movement part nailed down but when I move the opposite direction that the character is facing, it goes in that direction, but when I left go of "A, left movement" the characters texture will go back to the right, could anyone tell me how to fix this? I have two separate textures for both the character facing left & right.

Example:

Pic #1: Walking whilst holding "D" Pic #2: Walking whilst holding "A" Pic #3: When I stop holding "A"
ImageImageImage

My current code:
Image

I hope I made this fully understandable.

Re: I need help. ;(

Posted: Fri Aug 24, 2012 4:12 pm
by Nixola
You have to check what's the last key held down by using love.keyreleased), and use that to decide where the player's facing

Re: I need help. ;(

Posted: Fri Aug 24, 2012 6:48 pm
by Kadoba
Like Nixola said, you need to figure out what the last key held down is. Since you're only using two directions you can do it this way:

Code: Select all

local moveKey
local playerImages = {}
playerImages["idle"] = love.graphics.newImage("textures/character_idle.png")
playerImages["left"] = love.graphics.newImage("textures/character_left.png")
playerImages["right"] = love.graphics.newImage("textures/character_right.png")


function love.keypressed(key)
  if key == "left" or key == "right" then moveKey = key end
end

function love.keyreleased(key)
   if key == "left" or key == "right" then moveKey = nil end
   if key == "left" and love.keyboard.isDown("right") then moveKey = "right" end
   if key == "right" and love.keyboard.isDown("left") then moveKey = "left" end
end

function player_draw()
   love.graphics.setColor(255,255,255)
   love.graphics.draw(playerImages[moveKey] or playerImages["idle"], player.x, player.y)
end

Re: I need help. ;(

Posted: Sat Aug 25, 2012 5:45 am
by Lafolie
Wouldn't it make more sense to do that in keypressed rather than released?

Re: I need help. ;(

Posted: Sat Aug 25, 2012 8:39 am
by Nixola
No, because if you press and hold right, then press and hold left, then release lef love.keypressed can't know it and it will continue facing left

Re: I need help. ;(

Posted: Sat Aug 25, 2012 1:19 pm
by Juryo
I'm sorry to say, but it did the same thing as before. ;(

Re: I need help. ;(

Posted: Sat Aug 25, 2012 1:21 pm
by Nixola
Did you use that variable to draw the correct animation?

Re: I need help. ;(

Posted: Sat Aug 25, 2012 1:37 pm
by Juryo
Yes, I did.

Re: I need help. ;(

Posted: Sat Aug 25, 2012 1:45 pm
by kikito
Please post a .love file showing your problem.

Re: I need help. ;(

Posted: Sat Aug 25, 2012 8:03 pm
by Juryo
I don't understand why I would need to implement one, but here is the .zip. Just open it with love.exe ^^