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"
My current code:
I hope I made this fully understandable.
I need help. ;(
Re: I need help. ;(
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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: I need help. ;(
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. ;(
Wouldn't it make more sense to do that in keypressed rather than released?
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Re: I need help. ;(
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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: I need help. ;(
I'm sorry to say, but it did the same thing as before. ;(
Re: I need help. ;(
Did you use that variable to draw the correct animation?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: I need help. ;(
Please post a .love file showing your problem.
When I write def I mean function.
Re: I need help. ;(
I don't understand why I would need to implement one, but here is the .zip. Just open it with love.exe
- Attachments
-
- Insomnia BUG ONLY.zip
- (1.16 MiB) Downloaded 144 times
Who is online
Users browsing this forum: Bing [Bot] and 4 guests