0
votes

Im building this 2D scroller where there is a little space ship that just shoots missiles. I am able to shoot the missile at the correct speed, but after shooting the first one, the next missiles keep getting faster and faster.

Also when I shoot a missile, if i shoot another one before the first missile leaves the screen, the first missile disappears and only the second is visible.

Can someone help me out with this? thanks

Here is the code:

--///////////////////////////////////// LOAD FUNCTION /////////////////////////////////////--
function love.load()
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")

  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

 if love.keyboard.isDown("space") then
   spawnMissile()
 end

 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
--Function missile : 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
    table.insert(missiles, missile)


end
end
--/////////////////////////////////////END OF DRAW FUNCTION /////////////////////////////////////--
2

2 Answers

1
votes

It is really good idea to get through the lua book first.

The code has numerous issues. Most outstandingly:

function spawnMissile()
    missile = {}
    //lines skipped
    table.insert(missiles, missile)
end

creates a global variable named missile it definitely should be local missile = {}.

Then, all your loops are like:

for i, m in ipairs(missiles) do
   missile.x = missile.x + missileSpeed * dt
   //doing some stuff with missile
end

i and m are the variables that change throughout the for loop. i is an index, m is an element of the table stored at that index. Instead of working with m your code only processes the global variable missile which shouldn't even exist.

So, spawnMissile overwrites the missile on each call and love.update and love.draw only update and draw the missile. That's why only a single missile is present.


Additionally, let me link this guide here. It is long, it requires some knowledge of lua, I would do things differently, but it sure covers all things related to space ships that shoot missiles.

0
votes

First, you should take your spawnMissile function out of love.draw function. It must be alone after love.update function. It has to use dt too so that you can call it like spawnMissile( dt ) inside love.update. Once you have a table insert you also need table.remove the old shootings. Take a look on some YouTube tutorials. It will be so much easier to correct your code while you watch someone typing Lua lines. Plenty of them...