Going forward and backward with a caracter

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.
Solitaire
Prole
Posts: 12
Joined: Mon Jul 29, 2013 1:50 am

Going forward and backward with a caracter

Post by Solitaire »

Hello, muchachos;

I've got a problem. I'm xant to make a 2D scrolling game, but I can't figure out how to change the sprite when going in an opposite direction. Here's the code :

Code: Select all

require("AnAL")


function love.keypressed(key)
	if (key == "right") then
		anim:seek(1)
	end
end

function love.keypressed(key)
	if (key == "left") then
		anim:seek(14)
	end
end

function love.load()
	img = love.graphics.newImage("perso.png")
	bg = love.graphics.newImage("bg.png")
	anim = newAnimation(img, 170, 400, 0.1, 13)
	x = 0
end

function love.update(dt)
	if love.keyboard.isDown ("right") then
		anim:update(dt)
		x = x + 2
	
	elseif love.keyboard.isDown("left") then
		anim:update(dt)
		x = x - 2
	end
	
end

function love.draw()
	love.graphics.draw(bg)
	anim:draw(x, 100)
end
I've got a sprite sheet named "perso" containing 13 sprites of the character moving right, and 13 sprites of him moving left, so how do I do to switch sprites when changing the pressed button ?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Going forward and backward with a caracter

Post by raidho36 »

You can have another sprite flipped horizontally. Or you can flip it in such way during rendering.

Code: Select all

love.graphics.draw ( sprite, x, y, -1, 1 ) --latter two are X and Y scale, negative X scale does horizontal flipping. Play around with it to see how it works
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Going forward and backward with a caracter

Post by Kingdaro »

The fourth parameter for love.graphics.draw is rotation, so it'd be:

Code: Select all

love.graphics.draw ( sprite, x, y, 0, -1, 1 )
Selenaut
Prole
Posts: 6
Joined: Wed Jul 24, 2013 10:41 pm

Re: Going forward and backward with a caracter

Post by Selenaut »

I don't think rotation would help, as that would make the sprite appear upside-down. Solitaire wants the image to be flipped horizontally.
Making a game in Lua without knowing Lua.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Going forward and backward with a caracter

Post by raidho36 »

That's what he said. I simply forgot about third argument being rotation.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Going forward and backward with a caracter

Post by Kingdaro »

Yeah, why is the third argument rotation? That would imply that more people use just rotation than they use scaling and offsets, and I think this is far from the truth...
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Going forward and backward with a caracter

Post by mickeyjm »

Kingdaro wrote:Yeah, why is the third argument rotation? That would imply that more people use just rotation than they use scaling and offsets, and I think this is far from the truth...
I often use rotation and seldom use scaling actually
Your screen is very zoomed in...
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Going forward and backward with a caracter

Post by Jasoco »

Yeah. I'm more likely to scale using love.graphics.scale. But when I do scale from within the image I just set the rotation to 0. Easy peasy.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Going forward and backward with a caracter

Post by raidho36 »

According to what slime said, arguments order in drawing function is exactly the order specified transformations are applied before rendering. In following example, both snippets yield same result:

Code: Select all

love.graphics.draw ( sprite, x, y, r, sx, sy, ox, oy, kx, ky )

--====--

love.graphics.translate ( x, y )
love.graphics.rotate ( r )
love.graphics.scale ( sx, sy )
love.graphics.translate ( ox, oy )
love.graphics.shear ( kx, ky )
love.graphics.draw ( sprite ) --implying you can omit X and Y
If you don't certain arguments, you can just pass nil for them.
Solitaire
Prole
Posts: 12
Joined: Mon Jul 29, 2013 1:50 am

Re: Going forward and backward with a caracter

Post by Solitaire »

Well, now it's helpful for a rotation and stuff, but now, if I want it to jump, i'll have to "hide" the current sprite and replace it by another one. How do I achieve this ?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 2 guests