Help with Tables
Posted: Wed Jan 24, 2018 12:00 pm
I am creating a top down shooter, and I have created everything except the actual enemies, bullets and hit detection. I am trying to create the bullet system but when I have debugged to see if my for loop runs, it doesn't. This stops the bullets from being rendered. If anyone can see the issue, please let me know ASAP.
NOTE: Where I have written print is where the issues occur, as these for loops never run.
NOTE: Where I have written print is where the issues occur, as these for loops never run.
Code: Select all
bullet = {}
player = player or require("scripts.player");
Options = Options or require("scripts.options");
Game = Game or require("scripts.game");
bulletSpeed = 100;
image = love.graphics.newImage("images/bullet.jpg");
bullet.x = -10;
bullet.y = -10;
bullet.velX = 0;
bullet.velY = 0;
fire = love.audio.newSource("sounds/fire.mp3");
function bullet.load()
bulletTable = {}
end
function bullet.create(x, y, velX, velY)
bullet.x = x;
bullet.y = y;
bullet.velX = velX;
bullet.velY = velY;
end
b = 0;
function bullet.update(dt)
for i, v in ipairs(bulletTable) do
if v.remove then
table.remove(bulletTable, i);
print ("4");
i = i - 1;
else
v:update(dt);
print("5")
end
end
end
function bullet.spawn(dt)
if (love.mouse.isDown(1)) then
if (player.canShoot == true) then
if (GameState.getGameState() == "GAME" and (Game.game_time ~= 90 and Game.game_time ~= 180 and Game.game_timer ~= 300)) then
if (player.canShoot) then
if (Options.getSound() == "ON") then
fire:play();
end
velX = 0;
velY = 0;
table.insert(bulletTable, (Bullet.create(player.getX() + 23, player.getY() + 5, velX, velY)));
player.canShoot = false;
player.shootTime = 2;
end
end
end
end
end
function bullet.draw()
for i, v in ipairs(bulletTable) do
print("2");
v:render();
end
end
function bullet.render()
love.graphics.draw(image, v.x, v.y, 0, 1);
end
return bullet;