Page 1 of 1
Help with integers (SOLVED)
Posted: Tue Nov 26, 2013 12:23 am
by Pin222
Hi,
my question is; how do I display an integer?
my code is this:
Code: Select all
water = {}
function water.load()
waterGFX = {
love.graphics.newImage("waterbackground1.png"),
love.graphics.newImage("waterbackground2.png"),
}
waterFRAME = 1
end
function water.update(dt)
waterFRAME = waterFRAME + 10 * dt
if waterFRAME > 1 then waterFRAME = 0 end
end
function water.draw()
love.graphics.draw(waterGFX[waterFRAME],camera.x,camera.y + 256)
end
this code produces an error when reading waterFRAME variable on the "love.graphics.draw" as it needs to be read as an integer. How would I be able to display this variable as an integer value?
the aim is to have a animation with two frames and use the frame number (waterFRAME) as a counter and frame indicator
I feel stupid asking this as it seems such a simple thing, but I have looked on Google and these forums and surprisingly found nothing.
any help would be appreciated
Re: Help with integers
Posted: Tue Nov 26, 2013 12:31 am
by ejmr
Re: Help with integers
Posted: Tue Nov 26, 2013 12:49 am
by Boolsheet
Lua uses floating-point numbers for all numbers. The Lua users wiki has some information about Lua numbers
here and
here.
In your problem, you have to round the number down or up to get an integer. Lua provides the functions [manual]math.floor[/manual] and [manual]math.ceil[/manual] for this.
Code: Select all
if waterFRAME > 1 then waterFRAME = 0 end
This is problematic, it can never get over 1. Your waterbackground images are assigned to the keys 1 and 2 in the table so it would never be in the range of them.
You probably want to check if it is equal or bigger than 3 and then reset it to 1. Or even better, take the fraction plus 1 since that leftover fraction would get lost otherwise and the animation would look choppy.
Code: Select all
function water.update(dt)
waterFRAME = waterFRAME + 10 * dt
if waterFRAME >= 3 then
waterFRAME = waterFRAME - math.floor(waterFRAME) + 1 -- One way to get the fraction plus one.
end
end
function water.draw()
love.graphics.draw(waterGFX[math.floor(waterFRAME)],10, 10)
end
You could also look into the modulo operator. The remainder of a division has the property of staying in a certain range. We can exploit that here like this.
Code: Select all
function water.update(dt)
waterFRAME = waterFRAME + 10 * dt
end
function water.draw()
love.graphics.draw(waterGFX[math.floor(waterFRAME % 2) + 1],10, 10) -- Takes the remainder of waterFRAME divided by 2, rounds down and adds 1.
end
Re: Help with integers
Posted: Tue Nov 26, 2013 12:53 am
by Pin222
thank you for the quick response,
I have tried to implement this, here is my code:
Code: Select all
water = {}
function water.load()
waterGFX = {
love.graphics.newImage("waterbackground1.png"),
love.graphics.newImage("waterbackground2.png"),
}
waterFRAME = 1
end
function water.update(dt)
waterFRAME = waterFRAME + 10 * dt
if waterFRAME > 2 then waterFRAME = 1 end
end
function water.draw()
love.graphics.draw(waterGFX[tostring(waterFRAME)],camera.x,camera.y + 256)
end
but it produces the same error of
Code: Select all
incorrect parameter type: expected userdata
at the
Code: Select all
love.graphics.draw(waterGFX[tostring(waterFRAME)],camera.x,camera.y + 256)
line.
I also tried using the tonumber() with the same negative result.
not sure what the problem is here
Re: Help with integers
Posted: Tue Nov 26, 2013 7:39 am
by Roland_Yonaba
Code: Select all
water = {}
function water.load()
waterGFX = {
love.graphics.newImage("waterbackground1.png"),
love.graphics.newImage("waterbackground2.png"),
}
waterFRAME = 0
end
function water.update(dt)
waterFRAME = waterFRAME + 10 * dt
if waterFRAME > #waterFRAME then
waterFRAME = 0
end
end
function water.draw()
love.graphics.draw(waterGFX[math.ceil(waterFRAME)],camera.x,camera.y + 256)
end
How is this working for you ?
Re: Help with integers
Posted: Tue Nov 26, 2013 5:44 pm
by Pin222
Roland_Yonaba wrote:Code: Select all
water = {}
function water.load()
waterGFX = {
love.graphics.newImage("waterbackground1.png"),
love.graphics.newImage("waterbackground2.png"),
}
waterFRAME = 0
end
function water.update(dt)
waterFRAME = waterFRAME + 10 * dt
if waterFRAME > #waterFRAME then
waterFRAME = 0
end
end
function water.draw()
love.graphics.draw(waterGFX[math.ceil(waterFRAME)],camera.x,camera.y + 256)
end
How is this working for you ?
Thank you, yes this has worked.
strange though I tried this before and it didn't seem to work