Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 dementiapatient567   Members   -  Reputation: 106

Like
0Likes
Like

Posted 25 January 2013 - 03:25 PM

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.



Sponsor:

#2 FLeBlanc   Crossbones+   -  Reputation: 3117

Like
1Likes
Like

Posted 25 January 2013 - 04:16 PM

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.

#3 dementiapatient567   Members   -  Reputation: 106

Like
0Likes
Like

Posted 25 January 2013 - 04:23 PM

GAH I feel so stuid. Thanks! I don't know why I didn't think of that >.<






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS