Sign in to follow this  
Damien Musgrove

I can't seem to figure out why this isn't working(Lua/Love2D)

Recommended Posts

explosion= {}
explosion[1] = love.graphics.newImage("images/boom1.png") --images are in their proper directories.
explosion[2] = love.graphics.newImage("images/boom2.png")
explosion[3] = love.graphics.newImage("images/boom3.png")
explosion[4] = love.graphics.newImage("images/boom4.png")

animtimer = 0
eximg = explosion[1] --I've tried removing this
anum = 1 --dictates which image is used

function explode(dt) --This is just supposed to make the explosion images cycle through
animtimer = animtimer+dt 

eximg = eximg[anum] --I've tried moving this and removing it entirely. It just says that it's incorrect parameter type in the draw function.

if animtimer > 0.1 then --Just temp numbers and whatnot.
anum = anum+1
animtimer = 0
end
if anum > 4 then
anum = 1
end
end

function love.update(dt)
explode(dt)
end

function love.draw()
love.graphics.draw(eximg[anum],20,20) --It errors here saying that eximg is nil
end
 

Everything else in my "game" Is working just fine, but for some reason it's refusing to let me animate saying that a variable that clearly exists(I think...) is nil.

 

I'm sure it's just a stupid error but I can't seem to find it.

 

I've left it pretty heavily commented for this to make sure people can read my entry-level code.

Share this post


Link to post
Share on other sites
It looks like you are doing something wrong.

To start with, you assign eximg=explosion[1], so now eximg points to the first image in the list of explosion frames. But then in explode(), you assign eximg=eximg[anum]. This is the problem. eximg at this point isn't an array of anything, it's just a love.graphics.image type. So eximg[anum] (when anum is pretty much any number) will be equal to nil. So when you call explode(), you essentially set eximg equal to nil. Then when draw rolls around, you pass eximg[anum] to love.graphics.draw. This is tantamount to passing nil[anum], which is not allowed.

I think that what you are wanting to do instead is set eximg=explosion[anum] inside explode(), then in draw just call love.graphics.draw(eximg, 20, 20). This way, eximg gets pointed at the proper entry of explosion.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this