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 /////////////////////////////////////--