Page 1 of 1

[HELP] Shoot in one direction at a time

Posted: Mon May 25, 2015 10:55 pm
by zynerd
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:

Code: Select all

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
Any help would be very much appreciated! :)

Re: [HELP] Shoot in one direction at a time

Posted: Mon May 25, 2015 11:13 pm
by davisdude
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

Code: Select all

if player.directionFiring then
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.

Re: [HELP] Shoot in one direction at a time

Posted: Mon May 25, 2015 11:23 pm
by zynerd
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

Code: Select all

if player.directionFiring then
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.
Thank you very much! It's so simple, don't know why I couldn't think of that.

Re: [HELP] Shoot in one direction at a time

Posted: Mon May 25, 2015 11:34 pm
by zynerd
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

Code: Select all

if player.directionFiring then
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.
One last thing: Do you know how I could set a cooldown for the shooting?

Re: [HELP] Shoot in one direction at a time

Posted: Mon May 25, 2015 11:53 pm
by davisdude
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.

Re: [HELP] Shoot in one direction at a time

Posted: Tue May 26, 2015 12:04 am
by zynerd
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.
Awesome, thanks.