This is my code, why is the table nil? How do I fix my code?
displayMode(FULLSCREEN)
function setup()
physics.continuous = true
physics.gravity(0,-100)
ballTab={}
-- objects have a default value for collisions (0 or 1)
w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
w4 = physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
move(100,300,120,100,0)
end
function touched(t)
if t.state == BEGAN then
for a,b in pairs(ballTab) do
if t.x > b.x-b.size and t.x < b.x+b.size and t.y > b.y-b.size and t.y < b.y+b.size then
if a > 256 then
b:destroy()
else
s=b.size/2
vx=b.linearVelocity.x
vy=b.linearVelocity.y
move(b.x+s,b.y,s,100,vy+100) -- create 2 balls at half size
move(b.x-s,b.y,s,-100,vy+100)
b:destroy() -- destroy original
end
end
end
end
collectgarbage()
end
function move(posx,posy,size,vx,vy)
local p = physics.body(CIRCLE,size)
p.restitution = 1.01
p.linearVelocity = vec2(vx,vy)
p.x = posx
p.y = posy
p.mask={1} -- value of object to collide with
p.categories={2} -- value of this object
if size<5 then
size=5
end
p.size = size
p.bullet = true
p.friction=0
table.insert(ballTab,p) -- add ball to table
end
function draw()
background(40, 40, 50)
fill(164, 210, 223, 255)
for a,b in pairs(ballTab) do
ellipse(b.x,b.y,b.size*2) -- draw balls
end
text(1/DeltaTime,WIDTH/2,HEIGHT-25) -- fps
text(#ballTab,WIDTH/2,HEIGHT-50) -- number of balls
end