Simple math like this, ran a few too many times per second:
Code: Select all
local shouldDraw = (kk > math.floor((GameState:getPlayer().x-260)/16) and kk < math.floor(((GameState:getPlayer().x+260)/16)));
(I have drastically reduced this lag by only looping through and drawing tiles in the player's view, but it still comes back with some lines of code such as the above and below.)
For example, I go very far to the right of the level and very far down, and the lag spikes get worse and worse the further I get. Removing that line fixed it.
Another example, when I spawn more than just a couple bat entities of which this is the code:
Code: Select all
-- commented some stuff out for testing; nothing changed
function bat.new(x, y)
local self = setmetatable({}, bat)
self.x = x;
self.y = y;
self.batSprites = {};
self.box = aabb.new(true, 0, 0, 20, 14);
self.lastHit = 0;
self.health = 4;
self.dmg = 3;
--self.hitSound = love.audio.newSource("res/snd/enemyhit.wav");
--self.dieSound = love.audio.newSource("res/snd/explosion.wav");
self.isEnemy = true;
self.respawn = true;
return self;
end
function bat:keyPress() end
function bat:tick(dt)
if (self.dead) then return end;
--all tick code removed, still no difference in lag
end
function bat:draw()
if (self.dead) then return end;
-- all drawing code removed, still no difference in lag
end
return bat;
Code: Select all
-- and for gamestate object, updating and drawing respectively
for k, v in pairs(self.entities) do
if (v ~= nil) then
v:tick(dt);
end
end
for k, v in pairs(self.entities) do
if (v ~= nil) then
v:draw();
end
end
Keep in mind, my amount of entities [the amount of times my game has to loop and run a few functions] is very small, right now it doesn't even reach 20, and yet with the addition of a couple of bat entities everything goes wrong if I'm standing in the wrong place.
What should I do to figure out precisely what's causing any and all frame drops? I just don't get it.