Hi, I'm making a basic game in Love where the player can shoot bullets at enemies.
I'm trying to restrict the player to shooting in one direction at a time so the player can't spam bullets in all directions. Here's the part of the code I need modified:
function player.shoot(key)
if gameState == "playing" then
if key == 'up' then
bullet.spawn(player.x - bullet.width/2, player.y - player.height - bullet.height, 'up')
TEsound.play("/res/shoot.mp3")
end
if key == 'down' then
bullet.spawn(player.x - bullet.width/2, player.y + player.height + bullet.height, 'down')
TEsound.play("/res/shoot.mp3")
end
if key == 'right' then
bullet.spawn(player.x + player.width + bullet.width, player.y - bullet.width/2, 'right')
TEsound.play("/res/shoot.mp3")
end
if key == 'left' then
bullet.spawn(player.x - player.width - bullet.width, player.y - bullet.width/2, 'left')
TEsound.play("/res/shoot.mp3")
end
end
end
Make a variable called "player.directionFiring" or something like that. Inside of love.keypressed, if the key is one of the directions you have, set it to that direction. Then, inside of player.shoot you can say
And do the logic but with a variable instead.
Inside of love.keyReleased, if the key is one of the forum keys, set this variable to nil.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude wrote:Make a variable called "player.directionFiring" or something like that. Inside of love.keypressed, if the key is one of the directions you have, set it to that direction. Then, inside of player.shoot you can say
davisdude wrote:Make a variable called "player.directionFiring" or something like that. Inside of love.keypressed, if the key is one of the directions you have, set it to that direction. Then, inside of player.shoot you can say
Sure. Whenever you set player.directionFiring set another variable called player.shootTimer to whatever time you want. Inside of player.update subtract dt from the cooldown. Then inside of love.keypressed check if the cooldown is < 0 in order to fire.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude wrote:Sure. Whenever you set player.directionFiring set another variable called player.shootTimer to whatever time you want. Inside of player.update subtract dt from the cooldown. Then inside of love.keypressed check if the cooldown is < 0 in order to fire.