Page 1 of 1

Sprite speed

Posted: Tue Apr 23, 2024 7:34 pm
by Vixii
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

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
Sprite code

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)
1 enemy - https://gyazo.com/86d5a5e7a200cf52564e694c0f26b63f

5 enemies - https://gyazo.com/64218dae151d1fccea309af1599cd426

Re: Sprite speed

Posted: Wed Apr 24, 2024 5:07 am
by pgimeno
Hello, welcome to the forums. I may be wrong, but I think what's happening is that you have a single animation (animations.enemy) for all the enemies, and you update it once per enemy, therefore the more enemies you have, the more times in the same frame the animation will be updated.

If you're OK with a synchronized animation for all enemies, then you need to update it just once per frame, i.e. you can do animations.enemy:update(dt) out of the per-enemy loop.

If you want the animations to be independent, however, you need a new animation per enemy. You can do that by changing this:

Code: Select all

  enemy.animation = animations.enemy
to this:

Code: Select all

  enemy.animation = animations.enemy:clone()

Re: Sprite speed

Posted: Wed Apr 24, 2024 5:49 am
by Vixii
pgimeno wrote: Wed Apr 24, 2024 5:07 am Hello, welcome to the forums. I may be wrong, but I think what's happening is that you have a single animation (animations.enemy) for all the enemies, and you update it once per enemy, therefore the more enemies you have, the more times in the same frame the animation will be updated.

If you're OK with a synchronized animation for all enemies, then you need to update it just once per frame, i.e. you can do animations.enemy:update(dt) out of the per-enemy loop.

If you want the animations to be independent, however, you need a new animation per enemy. You can do that by changing this:

Code: Select all

  enemy.animation = animations.enemy
to this:

Code: Select all

  enemy.animation = animations.enemy:clone()
Thank you, that was the problem!

Re: Sprite speed

Posted: Wed Apr 24, 2024 9:14 pm
by dusoft
Vixii wrote: Wed Apr 24, 2024 5:49 am
pgimeno wrote: Wed Apr 24, 2024 5:07 am Hello, welcome to the forums. I may be wrong, but I think what's happening is that you have a single animation (animations.enemy) for all the enemies, and you update it once per enemy, therefore the more enemies you have, the more times in the same frame the animation will be updated.

If you're OK with a synchronized animation for all enemies, then you need to update it just once per frame, i.e. you can do animations.enemy:update(dt) out of the per-enemy loop.

If you want the animations to be independent, however, you need a new animation per enemy. You can do that by changing this:

Code: Select all

  enemy.animation = animations.enemy
to this:

Code: Select all

  enemy.animation = animations.enemy:clone()
Thank you, that was the problem!
Yeah, Lua can be tricky with its variables by reference. Deep copy is sometimes only option.

Re: Sprite speed

Posted: Mon Apr 29, 2024 2:46 am
by togFox
I would suggest making the jump from anim8 to rolling your own animation functions is not hard. If you're new to Lua or Love then keep doing what you're doing but consider learning about quads and tracking frames in your own table. You'll find stepping up to that level is not really hard and you'll have more options available to you.