Here is my code:
Code: Select all
function love.load()
meter = 64
love.physics.setMeter(meter)
world = love.physics.newWorld(0, 9.81*meter, true)
objects = {}
objects.ground = {}
objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2)
objects.ground.shape = love.physics.newRectangleShape(650, 50)
objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape)
love.graphics.setBackgroundColor(104, 136, 248)
love.window.setMode(650, 650)
options = {}
options.type = "box"
end
function addObj(type, x, y)
i = #objects
objects[i] = {}
objects[i].body = love.physics.newBody(world, x, y, "dynamic")
if type == "ball" then
objects[i].shape = love.physics.newCircleShape(25)
objects[i].fixture = love.physics.newFixture(objects[i].body, objects[i].shape, 1)
objects[i].fixture:setRestitution(0.9)
elseif type == "box" then
objects[i].shape = love.physics.newRectangleShape(0, 0, 50, 50)
objects[i].fixture = love.physics.newFixture(objects[i].body, objects[i].shape, 5)
end
end
function remObj(name)
objects[name] = nil
end
function love.update(dt)
world:update(dt)
end
function love.draw()
love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
local Count = 0
for Index, Value in pairs( objects ) do
Count = Count + 1
end
if Count > 1 then
for i=1,Count-1 do
love.graphics.setColor(50, 50, 50)
love.graphics.polygon("fill", objects[i].body:getWorldPoints(objects[i].shape:getPoints()))
end
end
love.graphics.setColor(255, 255, 255)
love.graphics.print(options.type, 10, 10)
love.graphics.print(Count, 10, 25)
end
function love.keypressed(key, isrepeat)
if not isrepeat then
if key == "right" then
options.type = "box"
elseif key == "left" then
options.type = "ball"
end
end
end
function love.mousepressed(x, y, button)
addObj(options.type,x,y)
end
Code: Select all
Error: main.lua:56: attempt to index a nil value
stack traceback:
main.lua:56: in function 'draw'
[string "boot.lua"]:438: in function <[string "boot.lua"]:399>
[C]: in function 'xpcall'
Thanks for the help!