Page 1 of 1

Quick Question. Generic for not updating all values.

Posted: Wed Oct 06, 2010 6:45 pm
by Angrycrow
I've been struggling with this for too long! There has to be a better solution. I'm having problems looping through a table of objects.

let's say I create a rock in my rocks table.

Code: Select all

function love.keypressed( key )

   if key == " " then 
   
		--newRock(x,y,weight)	
		table.insert(rocks,newRock(math.abs(love.mouse.getX()), -50,1))
		
   end
   
end
then I want to update the objects ... in love update

Code: Select all


function update(dt)

	for n, b in ipairs(rocks) do
		b:update(dt)
	end
	
end 

this works for moving the rocks and updating the instances.

But I start to run into problems when I want to hit multiple rocks. For some reason the following code will return true only on the last index of the rocks table.

Code: Select all


-- xan is the player

-- some is a debug var so we can see  if it's working. 

	for i,v in ipairs(rocks) do
	
		if checkHit(xan,v) then
			some = "you're hitting!"
		else
			some = "not hitting any"
		end 
	end

Does anyone know what would cause this? I've been banging my head against it for a little while. Leads, tips, links are all accepted and appreciated.

Re: Quick Question. Generic for not updating all values.

Posted: Wed Oct 06, 2010 6:50 pm
by Exasperation
You're overwriting 'some' too often in your loop. Try something like this:

Code: Select all

local cntr = 0
for i, v in ipairs(rocks) do
    if checkHit(xan,v) then
        cntr = cntr + 1
    end
end
some = string.format("Hitting %d rocks.", cntr)