Problem with a list of bullets

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Problem with a list of bullets

Post by tavuntu »

Hi everyone I'm trying to make a spaceship which must shot an indeterminated number of bullets.
I just started in lua and Love2D and I don't know a lot of things, the problem in this case, is that if a shot 1 bullet and this come
to the top, it disappear correctly and the game still runing well, but if I shot more than 1 bullet, when the first comes to the top of the screen I get the error:
"attempt to index field '?' (a nil value)" in the line 43.
This is the code:

Code: Select all

function love.load()
	enem={}
	gr=love.graphics
	kb=love.keyboard
	ancho=gr.getWidth()
	alto=gr.getHeight()
	minave={}
	minave.vel=150
	minave.img=gr.newImage("nave.png")
	minave.ancho=minave.img:getWidth()
	minave.alto=minave.img:getHeight()
	-- Centrar nave:
	minave.x=ancho/2-minave.img:getWidth()/2
	minave.y=alto/2-minave.img:getHeight()/2
	balas={}
	vel_bala=300
end

function love.draw()
	gr.setColor(255,255,255)
	gr.draw(minave.img,minave.x-minave.ancho/2,minave.y-minave.alto/2)
	local tam_cir=7
	if kb.isDown("down") then
		tam_cir=math.random(2,5)
	elseif kb.isDown("up") then
		tam_cir=math.random(9,18) 
	else
		tam_cir=math.random(5,10)
	end
	gr.setColor(255,255,0)
	gr.circle("fill",minave.x,minave.y+minave.alto/3,tam_cir)
	dibujarBalas()
end
function love.update(dt)
	if kb.isDown("left")  then minave.x=minave.x-(dt*minave.vel) end
	if kb.isDown("right") then minave.x=minave.x+(dt*minave.vel) end
	if kb.isDown("up")    then minave.y=minave.y-(dt*minave.vel) end
	if kb.isDown("down")  then minave.y=minave.y+(dt*minave.vel) end
	moverBalas(dt)
end
function moverBalas(dt)
	for i=1,#balas do
		balas[i].y=balas[i].y-(vel_bala*dt)   -- ERROR.
		if balas[i].y<30 then
			table.remove(balas,i)
		end
	end
end
function dibujarBalas()
	for i=1,#balas do
		gr.rectangle("fill",balas[i].x,balas[i].y,4,10)
	end
end
function love.keypressed(key)
	if key==" " then
		local bala=
		{
			x=minave.x,
			y=minave.y
		}
		table.insert(balas,bala)
	end
end
Thanks for your help. Sorry for the bad english.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Problem with a list of bullets

Post by micha »

The problem lies here:

Code: Select all

   for i=1,#balas do
      balas[i].y=balas[i].y-(vel_bala*dt)   -- ERROR.
      if balas[i].y<30 then
         table.remove(balas,i)
      end
   end
Lets say you have 5 bullets. Then i runs from 1 to 5. But when on the way you remove bullet no. 3 then the loop continues, but there are only 4 bullets left. So when i=5 your program tries to access balas[5], which does not exist.

To fix this, go through the loop backwards

Code: Select all

   for i=#balas,1,-1 do
      balas[i].y=balas[i].y-(vel_bala*dt)   -- ERROR.
      if balas[i].y<30 then
         table.remove(balas,i)
      end
   end
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Problem with a list of bullets

Post by tavuntu »

Thank you! I thought that size of tables updates automatically but nop. Saludos y muchas gracias :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests