Help with Changing a sprite using quads and love.update

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
Benni88
Prole
Posts: 10
Joined: Mon Oct 24, 2011 10:34 am

Help with Changing a sprite using quads and love.update

Post by Benni88 »

Hello,

I'm just in the process of trying to create my first game and im attempting to move the character along the x-axis and changing the sprite to indicate direction with each love.keyboard.isDown. I've set up a quad with different orientations of my character sprite using a technique i saw in a tutorial (https://github.com/kikito/love-tile-tutorial/wiki) but i am getting errors regarding which function to use in the love.update. If i use graphics.draw i get an error saying that im using the incorrect parameter, and i get a different error when trying to draw from the quad using love.drawq.

Attached is my .love file.

I'd greatly appreciate any help anyone could give, perhaps aswell as an explanation as to why it is incorrect? (my last post was answered so clearly!).

Many thanks,
Ben
Attachments
Benni's Project.love
(2.44 KiB) Downloaded 139 times
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Help with Changing a sprite using quads and love.update

Post by tentus »

1: Delete the ".lua" extension from your requires, bartbes changed the way that require will work in future versions of Love.
2: Don't call love.graphics.draw from love.update or any contained functions. Call love.graphics.draw from love.draw.
3: You're going to want to add max/min checks to your movement function. That is to say, make sure that if the player orientation dips below 1, it gets changed to 4, and vice versa.
Kurosuke needs beta testers
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: Help with Changing a sprite using quads and love.update

Post by adnzzzzZ »

There a few things that you got wrong, I think:

If you want to draw quads you'll need to use love.graphics.drawq in line 21.

Code: Select all

function love.draw()
	love.graphics.draw(Background,0,0)
	love.graphics.drawq(Char_Image, Player_Image, Player_x, Player_y)
end
You don't need to redraw the image in the movement function, you only need to change Player_Image and then when you call love.graphics.drawq(Char_Image, Player_Image, Player_x, Player_y) the correct quad will be drawn.

Code: Select all

function movement()
	if love.keyboard.isDown("left") then
		Player_Image = Char_Orientation[2]
		Player_x = (Player_x - 1)

	elseif love.keyboard.isDown("right") then
		Player_Image = Char_Orientation[4]
		Player_x = (Player_x + 1)
	end
end
And the most important thing: you are defining Player_{image, x, y} as locals and then redefining Player_{x, y} inside love.draw. You only need to define them once in love.load (not as locals) and then change them in the rest of your program.

Code: Select all

	
   ...
   Player_Image = Char_Orientation[1]
	Player_x = 50
	Player_y = 210
end
Benni88
Prole
Posts: 10
Joined: Mon Oct 24, 2011 10:34 am

Re: Help with Changing a sprite using quads and love.update

Post by Benni88 »

Thanks very much for your answers guys. Very helpful.
Benni88
Prole
Posts: 10
Joined: Mon Oct 24, 2011 10:34 am

Re: Help with Changing a sprite using quads and love.update

Post by Benni88 »

Hi guys. I followed your advice and got my character sprite changing and moving in a satisfactory manner, however when i tried to use the same approach (love.graphics.drawq) for an enemy sprite I'm getting the same Love error (Incorrect parameter type: expected Image). I don't understand why it doesnt work as I've used the same method and reviewed what i've written so far and it seems to be correct.

The image file i'm using is only split into 2 different sprites, would this have an impact?

Attached is a fresh copy of my .love.

Thanks if any of you can spare some time.
Attachments
Benni's Project.love
(3.83 KiB) Downloaded 126 times
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Help with Changing a sprite using quads and love.update

Post by miko »

Benni88 wrote: I don't understand why it doesnt work as I've used the same method and reviewed what i've written so far and it seems to be correct.
Change this:

Code: Select all

  love.graphics.drawq(Char_Image,Player_Image, Player_x, Player_y)
  love.graphics.drawq(Enemy_Image,Enemy_Image, Enemy_x, Enemy_y)
to this:

Code: Select all

  love.graphics.drawq(Char_Image,Player_Image, Player_x, Player_y)
  love.graphics.drawq(Char_Image,Enemy_Image, Enemy_x, Enemy_y)
drawq() need an image as a first argument, not a quad.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with Changing a sprite using quads and love.update

Post by bartbes »

Looking at this, I think this could have been prevented by naming the variables properly, you're calling a Quad an Image, and that's bound to go wrong sometime (this time!).
Benni88
Prole
Posts: 10
Joined: Mon Oct 24, 2011 10:34 am

Re: Help with Changing a sprite using quads and love.update

Post by Benni88 »

thanks guys. help is greatly appreciated.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot], Google [Bot], kov_serg and 8 guests