love.graphics.rectangle issue?

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
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

love.graphics.rectangle issue?

Post by kargon »

I'm probably doing this all wrong, however, I have a base_character.lua file drawing my character, and everything works fine. When I try to call its hitbox information, I get:

(number expected, got nil)

Code: Select all

	g.setColor(0, 200, 50, 100)
	g.rectangle("fill", 25, 25, 25, 25)  -- (this one works)
	print(ent.hitboxX, ent.hitboxY, ent.hitboxW, ent.hitboxH) -- added to prove the values are being passed through - they are)
--	g.rectangle("fill", ent.hitboxX, ent.hitboxY, ent.hitboxW, ent.hitboxH) -- will give error =(
The ent.hitboxX/Y/W/H values are coming from the same place my ent.alignX / ent.align.Y are, so that is what has me confused.

Any thoughts, solutions? Thanks!
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: love.graphics.rectangle issue?

Post by kargon »

Forgot to mention, this is my whole function ent:draw() in base_character.lua:

Note: the ent.playerAnim:draw works fine... and both ent.alignX/ent.alignY and all my ent.hitbox variables come from the same place. Yet I still get the (number expected, got nil).

Code: Select all

	local x = self.x
	local y = self.y
	g.setColor(0, 200, 50, 100)
	g.rectangle("fill", 25, 25, 25, 25)
	print(ent.hitboxX, ent.hitboxY, ent.hitboxW, ent.hitboxH)
--	g.rectangle("fill", ent.hitboxX, ent.hitboxY, ent.hitboxW, ent.hitboxH) -- this is the one I'm having trouble with!

	g.setColor(255, 255, 255, 255)
	ent.playerAnim:draw(ent.image, x, y, 0, self.facing, 1, ent.alignX, ent.alignY)

User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: love.graphics.rectangle issue?

Post by DaedalusYoung »

Could you please upload a .love file?
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: love.graphics.rectangle issue?

Post by kargon »

Here it is!
benGame.love
(139.88 KiB) Downloaded 303 times
Thanks for taking a look!

Note: It's very sloppy as I'm still learning about modules and inheritance. Each time I add something, I go back and remake the project from scratch and try to make it cleaner. Sorry if it's really messy!
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: love.graphics.rectangle issue?

Post by DaedalusYoung »

What's happening is that the ent.hitbox variables don't exist for the first frame yet. Only when the update function is called, they're added in the ent table. To solve, just add them in the load function in char_cap.lua, sort of like so:

Code: Select all

function ent:load(x, y)

-- Character Settings

	ent.name = "Captain Commando"

	ent.health = 1000

	ent.walkSpeed = 300
	ent.jumpSpeed = -750	-- negative moves towards top of screen

	ent.canJump = false
	ent.inAir = true

	ent.state = "idle"		-- default state

	ent.facing = -1
	ent.xOffset = 0
	ent.yOffset = -17	-- may be sloppy implementation for now...

	ent.width = 128		-- base collision size
	ent.height = 128	-- base collision size

	ent.myColor = {0, 200, 50}



	ent.alignX = 0
	ent.alignY = 0

-------------- Add them here, for example:

	ent.hitboxX = 1
	ent.hitboxY = 1
	ent.hitboxW = 1
	ent.hitboxH = 1

	ent.image = g.newImage('images/char/cap/cap.png')

	local g128 = anim8.newGrid(128, 128, ent.image:getWidth(1280), ent.image:getHeight(832))
	capIdle = anim8.newAnimation(g128(1, 1), 10)		-- slot 1, from row 1
	capWalkL = anim8.newAnimation(g128("1-10", 2), 0.08)	-- slot 1-10, from row 2
	capWalkR = anim8.newAnimation(g128("1-10", 2), 0.08)	-- slot 1-10, from row 2

	local g160 = anim8.newGrid(160, 128, ent.image:getWidth(1280), ent.image:getHeight(832))
	capDash = anim8.newAnimation(g160("1-8", 3, "1-3", 4), 0.08)	-- slot 1-10, from row 2

	local g160x2 = anim8.newGrid(160, 160, ent.image:getWidth(1280), ent.image:getHeight(832), 0, 512)
	capJump = anim8.newAnimation(g160x2("1-7", 1), 0.07, "pauseAtEnd")
	capFall = anim8.newAnimation(g160x2("8-8", 1, "1-3", 2), 0.15, "pauseAtEnd")

	ent.playerAnim = capFall

	print("Captain Commando: Created")

end
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: love.graphics.rectangle issue?

Post by kargon »

Ahh, thank you Daedalus! I'll try it out and let you know my results!
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: love.graphics.rectangle issue?

Post by kargon »

Worked great. Thanks so much. Once I looked at the error that you pointed out, it made so much more sense!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], zingo and 6 guests