Hi all.
I was playing around with an idea ( Codea! ) of mine using a @dave1707’s fantastic piece of code (a “physics shooter”), trying to shoot two simultaneous bullets at a big target. Everything was fine when I created the bullets one by one “by hand” (the code lines commented out below), but when I wanted to automatize the bullets’ creation with a “for loop”, I got the error message of “trying to index a nil value”. I do know that Lua tables are one the most striking features of the language and any help would be appreciated. Thanks in advance ~O)
displayMode(FULLSCREEN)
function setup()
m=0
-- b1={}
-- b2={}
b={}
lv=vec2(400,0)
cyl=physics.body(CIRCLE,30)
cyl.x=WIDTH/2
cyl.y=HEIGHT/2
cyl.type=STATIC
physics.gravity(0,0)
end
function draw()
local x,y
background(232, 232, 19, 255)
fill(255, 23, 0, 255)
ellipse(cyl.x,cyl.y,cyl.radius*2)
for z=1,m do
-- x=b1[z].x
-- y=b1[z].y
-- x2=b2[z].x
-- y2=b2[z].y
fill(0, 74, 255, 255)
for i=1,2 do
ellipse(b[i][z].x,b[i][z].y,10)
end
-- ellipse(x,y,10)
-- ellipse(x2,y2,10)
end
end
function shoot(x)
-- b1[x] = physics.body(CIRCLE,5)
-- b1[x].x=20
-- b1[x].y=HEIGHT/2-10
-- b1[x].linearVelocity=lv
-- b2[x]=physics.body(CIRCLE,5)
-- b2[x].x=20
-- b2[x].y=HEIGHT/2+10
-- b2[x].linearVelocity=lv
-- b[1]={}
-- b[2]={}
for i=1,2 do -- I tried this, but failed
b[i][x]=physics.body(CIRCLE,5)
b[i][x].x=20
b[i][x].y=HEIGHT/2-i*10
b[i][x].linearVelocity=lv
end
end
function touched(t)
if t.state == BEGAN then
if CurrentTouch.x > WIDTH/2 then
m = m + 1
shoot(m)
end
end
end
Yes, I should have written {} instead of nil, I wasn’t thinking straight, too early in the morning!
This works for me
function shoot(x)
for i=1,2 do
b[i]={} --the extra line
b[i][x]=physics.body(CIRCLE,5)
b[i][x].x=20
b[i][x].y=HEIGHT/2-i*10
b[i][x].linearVelocity=lv
end
end
Good morning!
Well, sorry, what I want to do is shooting with every screen touch. Inserting the afore mentioned line, it works just for the first shot, but not for the second shot (i.e. touch) and so on.
Edit: well, the error appears now when the draw function is called.
displayMode(FULLSCREEN)
function setup()
m=0
-- b1={}
-- b2={}
b={}
lv=vec2(400,0)
cyl=physics.body(CIRCLE,30)
cyl.x=WIDTH/2
cyl.y=HEIGHT/2
cyl.type=STATIC
physics.gravity(0,0)
end
function draw()
local x,y
background(232, 232, 19, 255)
fill(255, 23, 0, 255)
ellipse(cyl.x,cyl.y,cyl.radius*2)
for z=1,m do
fill(0, 74, 255, 255)
for i=1,2 do
if b[z][i] then --only continue if this bullet still exists
--destroy bulet if it goes off screen
if b[z][i].x<0 or b[z][i].x>WIDTH or b[z][i].y<0 or b[z][i].y>HEIGHT then
b[z][i]:destroy() --destroy physics object
b[z][i]=nil --remove item from table
else ellipse(b[z][i].x,b[z][i].y,10) end
end
end
end
end
function shoot(x)
b[x]={}
for i=1,2 do
b[x][i]=physics.body(CIRCLE,5)
b[x][i].x=20
b[x][i].y=HEIGHT/2-i*10
b[x][i].linearVelocity=lv
end
end
function touched(t)
if t.state == BEGAN then
if CurrentTouch.x > WIDTH/2 then
m = m + 1
shoot(m)
end
end
end
Hi! It’s morning now in Mexico. :-h @Ignatz, solved, brilliant! =D> Thanks a lot! ^:)^ In fact, I’ve been trying to understand with this your blog-post about coroutines and I have already a similar code using such features to compare with. More about this soon.
¡Saludos! By the way: your Spanish is good :)>-