Sprite speed

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Vixii
Prole
Posts: 2
Joined: Tue Apr 23, 2024 7:25 pm

Sprite speed

Post 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
User avatar
pgimeno
Party member
Posts: 3556
Joined: Sun Oct 18, 2015 2:58 pm

Re: Sprite speed

Post 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()
Vixii
Prole
Posts: 2
Joined: Tue Apr 23, 2024 7:25 pm

Re: Sprite speed

Post 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!
User avatar
dusoft
Party member
Posts: 514
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Sprite speed

Post 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.
User avatar
togFox
Party member
Posts: 782
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Sprite speed

Post 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.
Current project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life. Simple to learn, hard to master. Learn when to advance, dodge, strike and counter. Learn how to strike without being struck.
https://discord.gg/HeHgwE5nsZ
Post Reply

Who is online

Users browsing this forum: keharriso and 4 guests