Quick Question. Generic for not updating all values.

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
User avatar
Angrycrow
Prole
Posts: 29
Joined: Fri Mar 06, 2009 8:16 pm
Location: Los Angeles

Quick Question. Generic for not updating all values.

Post 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.
I like nonsense, it wakes up the brain cells. Fantasy is a necessary ingredient in living, It's a way of looking at life through the wrong end of a telescope. Which is what I do, And that enables you to laugh at life's realities.
- Dr. Seuss
User avatar
Exasperation
Prole
Posts: 11
Joined: Sat Oct 02, 2010 7:11 pm

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

Post 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)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 144 guests