Page 1 of 2

Going forward and backward with a caracter

Posted: Mon Jul 29, 2013 2:02 am
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 ?

Re: Going forward and backward with a caracter

Posted: Mon Jul 29, 2013 4:15 am
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

Re: Going forward and backward with a caracter

Posted: Mon Jul 29, 2013 10:43 am
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 )

Re: Going forward and backward with a caracter

Posted: Tue Jul 30, 2013 2:06 am
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.

Re: Going forward and backward with a caracter

Posted: Tue Jul 30, 2013 4:23 am
by raidho36
That's what he said. I simply forgot about third argument being rotation.

Re: Going forward and backward with a caracter

Posted: Tue Jul 30, 2013 12:14 pm
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...

Re: Going forward and backward with a caracter

Posted: Tue Jul 30, 2013 12:21 pm
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

Re: Going forward and backward with a caracter

Posted: Tue Jul 30, 2013 11:49 pm
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.

Re: Going forward and backward with a caracter

Posted: Wed Jul 31, 2013 4:11 am
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.

Re: Going forward and backward with a caracter

Posted: Sat Aug 10, 2013 8:19 pm
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 ?