Everything works perfectly except when I try to make the coins 'respawn', the mess is in the startRespawn function.
I have no idea about how to solve it, I probably f up some basic logic/rule?
I'm calling all the .draw,.update ect in the main and passing dt, but as soon as I collide with a coin I get the error
" attempt to perfrom arithmetic on local 'dt' (a nil value)"
Code: Select all
Coin = {}
Coin.__index = Coin
ActiveCoins = {}
function Coin.new(x,y)
instance = setmetatable({}, Coin)
instance.x = x
instance.y = y
instance.img = love.graphics.newImage("IMG/diamond.png")
instance.width = instance.img:getWidth()
instance.height = instance.img:getHeight()
instance.scaleX = 1
instance.toBeRemoved = false
instance.respawnTimer = 0
instance.respawn = false
instance.physics = {}
instance.physics.body = love.physics.newBody(world, instance.x, instance.y, 'static')
instance.physics.shape = love.physics.newRectangleShape(instance.width, instance.height)
instance.physics.fixture = love.physics.newFixture(instance.physics.body, instance.physics.shape)
instance.physics.fixture:setSensor(true)
table.insert(ActiveCoins, instance)
end
function Coin:startRespawn(dt)
for i,instance in ipairs(ActiveCoins) do
if instance == self then
if self.toBeRemoved then
self.respawnTimer= self.respawnTimer + dt
if self.respawnTimer > 5 then
Coin.new(self.x, self.y)
self.respawnTimer = 0
end
end
end
end
end
function Coin:remove()
for i,instance in ipairs(ActiveCoins) do
if instance == self then
Player.coins = Player.coins + 1
self.physics.body:destroy()
table.remove(ActiveCoins, i)
end
end
end
function Coin:spin(dt)
self.scaleX = math.sin( love.timer.getTime() * 1.5 )
end
function Coin:update(dt)
self:spin(dt)
self:startRespawn(dt)
self:checkRemove(dt)
end
function Coin:checkRemove(st)
if self.toBeRemoved then
self:remove( )
end
end
function Coin:draw()
love.graphics.draw(self.img, self.x, self.y, 0,self.scaleX,1, self.width/2, self.height/2)
end
function Coin:updateAll( dt )
for i,instance in ipairs(ActiveCoins) do
instance:update(dt)
end
end
function Coin:drawAll( )
for i,instance in ipairs(ActiveCoins) do
instance:draw()
end
end
function Coin.beginContact(a, b, collision)
for i,instance in ipairs(ActiveCoins) do
if a == instance.physics.fixture or b == instance.physics.fixture then
if a == Player.physics.fixture or b == Player.physics.fixture then
instance.toBeRemoved = true
return true
end
end
end
end