Before I made it destroy its self it worked fine. After I added it it would work fine until the first bullet destroys its self. When that happens it destroys its self and all the other bullets except the second one and won't let me shoot more. Have tried many things but can't find the error. Can someone help me?
Here is the code to spawn in the bullet:
Code: Select all
function love.mousepressed(x, y, button, istouch, presses)
if CheckGameState("Running") then
if button == 1 then
game.State.Running.Bullets[#game.State.Running.Bullets + 1] = bullet(5, 1, game.Weapons.Type.Pistol, "Sprites/Ammo/PistolAmmo.png", 200, 100, (#game.State.Running.Bullets + 1))
-- table.insert(game.State.Running.Bullets, (#game.State.Running.Bullets + 1), bullet(5, 1, game.Weapons.Type.Pistol, "Sprites/Ammo/PistolAmmo.png", 200, 100, (#game.State.Running.Bullets + 1)))
end
--Rest of function
Code: Select all
for i in pairs(game.State.Running.Bullets) do
game.State.Running.Bullets[i]:Move(dt)
if game.State.Running.Bullets[i]:CheckTouched() == true then
table.remove(game.State.Running.Bullets, game.State.Running.Bullets[i].place, game.State.Running.Bullets[i])
end
end
Code: Select all
local love = _G.love
function Bullet(dmg, id, Weapon, ImagePath, speed, distance, place)
local Image = love.graphics.newImage(ImagePath)
local MouseX, MouseY = love.mouse.getPosition()
local angle = math.atan2(MouseY - Weapon.Player.y, MouseX - Weapon.Player.x)
return {
dmg = dmg,
id = id,
Weapon = Weapon,
Image = Image,
x = nil,
y = nil,
angle = angle,
speed = speed,
distance = distance,
place = place,
Draw = function(self)
if id == Weapon.Player.WeaponId then
local MouseX, MouseY = love.mouse.getPosition()
if self.x == nil and self.y == nil then
-- local x = (self.Weapon.Player.x + math.cos(self.angle) * self.Weapon.Distance)
local x = nil
local y = nil
if math.deg(angle) <= 180 and math.deg(angle) >= 0 and false then
x = (self.Weapon.Player.x + math.cos(self.angle) * self.Weapon.Distance) + 5
y = (self.Weapon.Player.y + math.sin(self.angle) * self.Weapon.Distance)
elseif math.deg(angle) <= 45 and math.deg(angle) >= -90 and false then
y = (self.Weapon.Player.y + math.sin(self.angle) * self.Weapon.Distance)
x = (self.Weapon.Player.x + math.cos(self.angle) * self.Weapon.Distance) - 5
elseif math.deg(angle) <= 90 and math.deg(angle) >= -90 and true then
x = (self.Weapon.Player.x + math.cos(self.angle) * self.Weapon.Distance)
y = (self.Weapon.Player.y + math.sin(self.angle) * self.Weapon.Distance) - 5
else
x = (self.Weapon.Player.x + math.cos(self.angle) * self.Weapon.Distance)
y = (self.Weapon.Player.y + math.sin(self.angle) * self.Weapon.Distance) + 5
end
self.x = x
self.y = y
else
local x = self.x
local y = self.y
self.x = x
self.y = y
end
love.graphics.draw(self.Image, self.x, self.y, angle)
return
else
return
end
end,
Move = function(self, dt)
local run = false
pcall(function()
local MouseX, MouseY = love.mouse.getPosition()
local x = self.speed * dt * math.cos(self.angle)
local y = self.speed * dt * math.sin(self.angle)
self.x = self.x + x
self.y = self.y + y
run = true
end)
if run then
if self.distance > 0 then
self.distance = self.distance - 1
return false
elseif not (self.distance == nil) then
return true
else
return false
end
else
return false
end
end,
CheckTouched = function(self)
local run = false
pcall(function()
local MouseX, MouseY = love.mouse.getPosition()
local x = 0
self.x = self.x + x
run = true
end)
if run then
if self.distance > 0 then
self.distance = self.distance - 1
return false
elseif not (self.distance == nil) then
return true
else
return false
end
else
return false
end
end,
}
end
return Bullet