Here are some sidenotes:
In the main loop (in the update-function) you call the function escape and inside there you redefine the function love.keypressed. You do this each frame. It is enough to do this once. So put the definition of love.keypressed directly into the main.lua (that also makes it easier to find where the error is)
The bullet loop is like this:
Code: Select all
for i,v in ipairs(bullet) do
[...]
if v.x > 1500 or v.x < 0 - v.width or v.y > 900 or v.y < 0 - v.width then
table.remove(bullet, i)
end
end
That is likely to cause problems. The for loop traverses the numbers from 1 to n. But if you remove bullet i (in the center) then you change the table "bullet" while you loop over it. The save way is this (move backwards through the bullets):
Code: Select all
for i = #bullet,1,-1 do
v = bullet[i]
[...]
if v.x > 1500 or v.x < 0 - v.width or v.y > 900 or v.y < 0 - v.width then
table.remove(bullet, i)
end
end
Now for the error:
The original error of your code is in enemy.lua:
Code: Select all
while player.xCenter > v.x do
v.xvel = v.xvel + v.speed*dt
end
This is an infinite while loop. The condition itself does not change so the while loop will never finish. You need an if-condition here:
Code: Select all
if player.xCenter > v.x then
v.xvel = v.xvel + v.speed*dt
end
Here is the corrected enemiesUpdate (note, I also added two lines at the end and I corrected a wrong sign in the acceleration part. Please compare carefully with your original code)
Code: Select all
function enemiesUpdate(dt)
for i,v in ipairs(enemies) do
v.xvel = v.xvel * (1 - math.min(dt*v.friction, 1))
if player.xCenter > v.x then
v.xvel = v.xvel + v.speed*dt
end
if player.xCenter < v.x then
v.xvel = v.xvel - v.speed*dt
end
if player.yCenter > v.y then
v.yvel = v.yvel + v.speed*dt
end
if player.yCenter < v.y then
v.yvel = v.yvel - v.speed*dt
end
v.x = v.x+v.xvel
v.y = v.y+v.yvel
end
end