Page 1 of 1

Bullets gain speed and dissapear when another is shot

Posted: Sun May 20, 2018 9:13 pm
by luisNunes
Hi there! I have just started learning Lua and Love2D and i have been following some tutorials and also trying to do somethings myself (that's why sometimes it goes wrong). I'm doing this simple 2D shooter where a little space ship just shoots missiles. Everything works fine except:

- When I shoot the first missile, it goes at the speed i want it to go, but the more i shoot, the faster they go... i can't seem to find a way to maintain the same speed.

- If I fire one missile, and then after I shoot another one, the first missile dissapears before reaching the end of the X axis.

It would be great if someone could lead me in the right direction. Thanks!

Code: Select all

--///////////////////////////////////// LOAD FUNCTION /////////////////////////////////////--
function love.load()
  sound = love.audio.newSource("sounds/pew.wav", "static")
sprites = {}
  sprites.sky = love.graphics.newImage("sprites/sky.png")
  sprites.plane = love.graphics.newImage("sprites/tiny_ship.png")
  sprites.missile = love.graphics.newImage("sprites/missile.png")

  cooldown = 0

  spaceShip = {}
  spaceShip.x = 150
  spaceShip.y = 100
  spaceShipSpeed = 150

  missiles = {}
  end
--///////////////////////////////////// UPDATE FUNCTION /////////////////////////////////////--
function love.update(dt)
  if love.keyboard.isDown("down") then
    spaceShip.y = spaceShip.y + spaceShipSpeed * dt
  end
  if love.keyboard.isDown("up") then
    spaceShip.y = spaceShip.y - spaceShipSpeed * dt
  end
  if love.keyboard.isDown("left") then
    spaceShip.x = spaceShip.x - spaceShipSpeed * dt
  end
  if love.keyboard.isDown("right") then
    spaceShip.x = spaceShip.x + spaceShipSpeed * dt
  end

  cooldown = math.max(cooldown - dt, 0)
    if love.keyboard.isDown("space") and cooldown == 0 then
      cooldown = 0.2
      spawnMissile()
      sound:stop()
      sound:play()


 end
-- this for loop makes the missile move in the X axis--
 for i, m in ipairs(missiles) do
   missile.x = missile.x + missileSpeed * dt


    end

end
-- /////////////////////////////////////END OF UPDATE FUNCTION/////////////////////////////////////--

--/////////////////////////////////////DRAW FUNCTION///////////////////////////////////// --
function love.draw()
  love.graphics.draw(sprites.sky, 0, 0, nil, 0.5, 0.5)
  love.graphics.draw(sprites.plane, spaceShip.x , spaceShip.y, nil, nil, nil, sprites.plane:getWidth()/2, sprites.plane:getHeight()/2)


--puts the missile sprite in the same position as the spaceship--

  for i,m in ipairs(missiles) do
    love.graphics.draw(sprites.missile, missile.x, missile.y, nil, nil, nil, sprites.missile:getWidth()/2, sprites.missile:getHeight() /2)

  end
end
--/////////////////////////////////////END OF DRAW FUNCTION /////////////////////////////////////--
--Function spawnMissile : sets variables for missile location and speed and updates them in the Missiles Table whenever a missile is shot--
  function spawnMissile()
    missile = {}
    missile.x = spaceShip.x
    missile.y = spaceShip.y
    missileSpeed = 50
    missileShot = true
    table.insert(missiles, missile)
end

Re: Bullets gain speed and dissapear when another is shot

Posted: Sun May 20, 2018 10:04 pm
by MrFariator
Couple of notes:
- You are not using the 'local' keyword inside your spawnMissile function. Because lua is global by default, this has the effect of you overriding the global 'missile' table every time a missile is spawned, effectively replacing whatever it was holding before (in this case, replacing a missile that was launched before). You can fix this by just adding the relevant keyword like so:

Code: Select all

function spawnMissile()
    local missile = {} -- local here
    missile.x = spaceShip.x
    missile.y = spaceShip.y
    missile.speed = 50 -- this could be a global or per-missile value, up to you
    missileShot = true
    table.insert(missiles, missile)
end
- When you iterate the missiles in order to update them, handle it like this:

Code: Select all

for i, m in ipairs(missiles) do 
   m.x = m.x + m.speed * dt -- "m" holds the missile instance's table, so lets modify it instead of the 'missile' global
end
With those couple of fixes you should get rid of disappearing missiles. For future reference, you can use strict.lua to catch any accidental global variables.

Re: Bullets gain speed and dissapear when another is shot

Posted: Sun May 20, 2018 11:18 pm
by luisNunes
Ah yes! Now its working properly. Although there was something else i had to change.

Code: Select all

for i,m in ipairs(missiles) do
    love.graphics.draw(sprites.missile, missile.x, missile.y, nil, nil, nil, sprites.missile:getWidth()/2, sprites.missile:getHeight() /2)

  end


Correct code below

Code: Select all

for i,m in ipairs(missiles) do
    love.graphics.draw(sprites.missile, m.x, m.y, nil, nil, nil, sprites.missile:getWidth()/2, sprites.missile:getHeight() /2)

  end
Had to change missile.x and missile.y to m.x and m.y

Thank you for your help!