Problem with a list of bullets
Posted: Tue Apr 23, 2013 11:53 pm
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:
Thanks for your help. Sorry for the bad english.
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