Page 1 of 1

love.graphics.rectangle issue?

Posted: Sat Sep 13, 2014 6:48 pm
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!

Re: love.graphics.rectangle issue?

Posted: Sat Sep 13, 2014 7:13 pm
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)


Re: love.graphics.rectangle issue?

Posted: Sat Sep 13, 2014 7:29 pm
by DaedalusYoung
Could you please upload a .love file?

Re: love.graphics.rectangle issue?

Posted: Sat Sep 13, 2014 7:38 pm
by kargon
Here it is!
benGame.love
(139.88 KiB) Downloaded 304 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!

Re: love.graphics.rectangle issue?

Posted: Sat Sep 13, 2014 8:00 pm
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

Re: love.graphics.rectangle issue?

Posted: Sat Sep 13, 2014 11:44 pm
by kargon
Ahh, thank you Daedalus! I'll try it out and let you know my results!

Re: love.graphics.rectangle issue?

Posted: Sun Sep 14, 2014 12:41 am
by kargon
Worked great. Thanks so much. Once I looked at the error that you pointed out, it made so much more sense!