Bullets gain speed and dissapear when another is shot

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
luisNunes
Prole
Posts: 2
Joined: Sun May 20, 2018 8:46 pm

Bullets gain speed and dissapear when another is shot

Post 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
MrFariator
Party member
Posts: 511
Joined: Wed Oct 05, 2016 11:53 am

Re: Bullets gain speed and dissapear when another is shot

Post 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.
luisNunes
Prole
Posts: 2
Joined: Sun May 20, 2018 8:46 pm

Re: Bullets gain speed and dissapear when another is shot

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 69 guests