From a forum post a little while ago ill link below this says, I must keep the list out of the function to remove it, but I have no clue of how to do that when dealing with class's since im world:add(self) and world:remove(self)
Link : viewtopic.php?f=4&t=86556&p=226785&hili ... ed#p226785
Here is the code incase it can help
Bullet.lua-
Code: Select all
Bullet = Object:extend()
function Bullet:new(x, y)
self.r = 1
self.x = x
self.y = y
self.speed = 700
self.w = 10
self.h = 10
self.path = 0
self.vx = 0
self.vy = 0
self.dead = false
if player.d == 4 then
self.path = 4
elseif player.d == 1 then
self.path = 1
elseif player.d == 2 then
self.path = 2
elseif player.d == 3 then
self.path = 3
end
world:add(self, self.x, self.y, self.w, self.h)
end
function Bullet:update(dt)
self.vx = 0
self.vy = 0
if self.x > screenx or self.x < 0 or self.y > screeny or self.y < 0 then
self.dead = true
end
if self.path == 4 then
self.vy = self.vy + self.speed * dt
elseif self.path == 1 then
self.vx = self.vx - self.speed * dt
elseif self.path == 2 then
self.vx = self.vx + self.speed * dt
elseif self.path == 3 then
self.vy = self.vy - self.speed * dt
end
self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)
local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
local newX, newY = world:move(self, targetX, targetY)
if newX == targetX and newY == targetY then -- no collision
self.x, self.y = newX, newY
else -- collision
self.dead = true
end
if self.dead == true then
world:remove(self)
end
end
function Bullet:draw()
if self.dead == true then
love.graphics.circle("line",self.x,self.y,29)
end
--love.graphics.circle("fill", self.x, self.y,self.r)
love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end