[HELP] Shoot in one direction at a time

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

[HELP] Shoot in one direction at a time

Post 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! :)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post 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.
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
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

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

Post 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.
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

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

Post 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?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post 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.
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
zynerd
Prole
Posts: 7
Joined: Sun May 24, 2015 10:19 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests