Sprite speed
Posted: Tue Apr 23, 2024 7:34 pm
I'm new in love2d and I had a problem.
If I spawn more than one enemy, it multiplies the sprite's speed. Does anyone know why?
Enemy code
Sprite code
1 enemy - https://gyazo.com/86d5a5e7a200cf52564e694c0f26b63f
5 enemies - https://gyazo.com/64218dae151d1fccea309af1599cd426
If I spawn more than one enemy, it multiplies the sprite's speed. Does anyone know why?
Enemy code
Code: Select all
enemies = {}
function spawnEnemy(x, y)
local enemy = world:newRectangleCollider(x, y, 22, 32, {collision_class = "Danger"})
enemy.dir = 1
enemy.speed = 100
enemy.animation = animations.enemy
enemy.scale = 1.2
table.insert(enemies, enemy)
end
function updateEnemies(dt)
for i, e in ipairs(enemies) do
e.animation:update(dt)
local ex, ey = e:getPosition()
local colliders = world:queryRectangleArea(ex + (11 * e.dir), ey + 16, 10, 10, {'Plataform'})
if #colliders == 0 then
e.dir = e.dir * -1
end
e:setX(ex + e.speed * dt * e.dir)
end
end
function drawEnemies()
for i, e in ipairs(enemies) do
local ex, ey = e:getPosition()
e.animation:draw(sprites.enemySheet, ex, ey, nil, e.scale * e.dir, e.scale, 17, 19)
end
end
Code: Select all
--- enemys
sprites.enemySheet = love.graphics.newImage('sprites/enemy.png')
local enemyGrid = anim8.newGrid(32, 32, sprites.enemySheet:getWidth(), sprites.enemySheet:getHeight())
animations.enemy = anim8.newAnimation(enemyGrid('1-6', 1), 0.07)
5 enemies - https://gyazo.com/64218dae151d1fccea309af1599cd426