Love2d Mirror/Flip Drawn Image.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Garb
Prole
Posts: 29
Joined: Fri Aug 05, 2011 8:47 pm

Love2d Mirror/Flip Drawn Image.

Post 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! :D
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Love2d Mirror/Flip Drawn Image.

Post by dreadkillz »

Use a negative scale for the image you want to draw w/ love.graphics.draw
Garb
Prole
Posts: 29
Joined: Fri Aug 05, 2011 8:47 pm

Re: Love2d Mirror/Flip Drawn Image.

Post 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
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Love2d Mirror/Flip Drawn Image.

Post 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) 
Garb
Prole
Posts: 29
Joined: Fri Aug 05, 2011 8:47 pm

Re: Love2d Mirror/Flip Drawn Image.

Post by Garb »

Thanks so much! You're awesome :D
Garb
Prole
Posts: 29
Joined: Fri Aug 05, 2011 8:47 pm

Re: Love2d Mirror/Flip Drawn Image.

Post 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?
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Love2d Mirror/Flip Drawn Image.

Post 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
Garb
Prole
Posts: 29
Joined: Fri Aug 05, 2011 8:47 pm

Re: Love2d Mirror/Flip Drawn Image.

Post by Garb »

I can't thank you guys enough you've both been a huge help :D
Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests