- 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