Help with integers (SOLVED)

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
Pin222
Prole
Posts: 5
Joined: Thu Nov 21, 2013 2:32 pm

Help with integers (SOLVED)

Post 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
Last edited by Pin222 on Tue Nov 26, 2013 5:44 pm, edited 1 time in total.
If you like to shoot pixels with pixels using pixels check out my game progress here: http://love2d.org/forums/viewtopic.php?f=5&t=76060
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Help with integers

Post by ejmr »

ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Help with integers

Post 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
Shallow indentations.
Pin222
Prole
Posts: 5
Joined: Thu Nov 21, 2013 2:32 pm

Re: Help with integers

Post 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
If you like to shoot pixels with pixels using pixels check out my game progress here: http://love2d.org/forums/viewtopic.php?f=5&t=76060
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Help with integers

Post 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 ?
Pin222
Prole
Posts: 5
Joined: Thu Nov 21, 2013 2:32 pm

Re: Help with integers

Post 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
If you like to shoot pixels with pixels using pixels check out my game progress here: http://love2d.org/forums/viewtopic.php?f=5&t=76060
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 1 guest