Page 1 of 1
Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 12:56 am
by Garb
I feel like this should be rather simple and I'm over-complicating it, but what's the easiest way to flip an image in love2d? Such as a platformer. The player is facing a right direction, then when you press the left key, his draw image gets flipped left. Such as updating in love.update.
thanks!
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 1:07 am
by dreadkillz
Use a negative scale for the image you want to draw w/ love.graphics.draw
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 1:32 am
by Garb
How exactly would I set this up?
(In love.update)
Code: Select all
function love.load()
love.graphics.setBackgroundColor(100, 149, 237)
x = 50
y = 50
speed = 100
playerIdle = love.graphics.newImage("/player/player_idle.png")
playerLeft = love.graphics.newImage("/player/player_walk_2.png")
playerRight = love.graphics.newImage("/player/player_walk_2.png")
playerDown = love.graphics.newImage("/player/player_walk_2.png")
playerUp= love.graphics.newImage("/player/player_walk_2.png")
player = playerIdle -- the player starts looking to the left
end
function love.update(dt)
if love.keyboard.isDown("d") then
x = x + (speed * dt)
player = playerRight
elseif love.keyboard.isDown("a") then
x = x - (speed * dt)
player = playerLeft
-- Here
player = love.graphics.scale(-5, -5 )
--This Just breaks it! D:
else
player = playerIdle
end
if love.keyboard.isDown("s") then
y = y + (speed * dt)
player = playerDown
elseif love.keyboard.isDown("w") then
y = y - (speed * dt)
player = playerUp
end
end
function love.draw()
love.graphics.draw(player, x, y)
end
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 1:42 am
by Kadoba
Code: Select all
-- normal
love.graphics.draw(image, x, y, rotation, 1, 1)
-- mirror
love.graphics.draw(image, x, y, rotation, -1, 1)
-- flipped
love.graphics.draw(image, x, y, rotation, 1, -1)
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 1:52 am
by Garb
Thanks so much! You're awesome
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 1:58 am
by Garb
Sorry but one last question, why is it that when he rotates he jumps a bit? like, when he scales right he goes like a whole bunch of pixels in that direction? How can I fix this?
More as, how do I chose where I want him to flip from?
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 2:13 am
by dreadkillz
When you flip or rotate an image, it does it about its pivotal point, which is the top left corner of the image. You need to offset your image so that its in the same location as its original state.
You should do this when you flip your image:
Code: Select all
function love.load ()
...
width = playerLeft:getWidth()
...
end
function love.draw()
...
-- flip left
love.graphics.draw( image, x, y, 0, -1, 1, width, 0)
...
end
Re: Love2d Mirror/Flip Drawn Image.
Posted: Tue Jun 12, 2012 2:19 am
by Garb
I can't thank you guys enough you've both been a huge help