Can't get my bullets to work :(

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
superdajk
Prole
Posts: 3
Joined: Tue Feb 21, 2012 7:58 pm
Location: The Netherlands

Can't get my bullets to work :(

Post by superdajk »

Hi there,

I'm kinda new to Lua and Löve2D but I intend to use it a lot because I like it so far. Anyhow, I've been working on this little prototype for my class tomorrow but I can't get my bullets to behave like I think they should!

My project is now basically this:

main.lua creates to instances of the class Player, one for player one (playerOne) and one for player two (playerTwo). This is working great. Now both players need to be able shoot some bullets (this is where the problem arises). I created a bullet class which for all intends and purposes is fine for now. This class is instantiated at the Player class, so I get two instances called playerOne.bullet and playerTwo.bullet. I have tried many things, but I can't even get them to at the least show themselves. They're just invisible! I do know they're there though, because they got their own coordinates. I would be endlessly thankful if someone could heb me with this :-).

My second conundrum is how I should do the bullets after this. Getting their behavior good won't be a problem but I guess I should put them in a table but I have no idea how to this exactly in Lua. Again, I would be very, very thankful if someone could explain this to me :-).

Thanks in advance!

Peter
Attachments
loveADP_1.love
(35.14 KiB) Downloaded 106 times
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Can't get my bullets to work :(

Post by Ellohir »

You aren't using the "Proyectile" functions anywhere. There are no proyectiles. You must create the bullets somewhere, the same way you called "Player" to create "PlayerOne" and "PlayerTwo".


PS: Maybe you should try without HardonCollider and HUMP, to start with. This seems fairly easy to be done without the libraries, and maybe it would be easier for you :nyu:
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Can't get my bullets to work :(

Post by MarekkPie »

He is using projectiles. It's in Player:update(dt), but that's also where your problem is.

You're calling Projectile:draw() inside a function which isn't love.draw(). The only way that something ever gets drawn onto the screen is by getting called in love.draw().

You also are getting a few other things mixed up. I'm assuming you want to add a new bullet to the self.bullets collection, but you are actually creating a new variable called self.bullet (with no s). You would want to do something like this:

Code: Select all

self.bullets[#self.bullets + 1] = Projectile:new(self.num)
self.bullets[#self.bullets]:initialize()
Now, you are adding a new bullet into the self.bullets table. Next, inside Player:update(dt), but outside any if statements, do this:

Code: Select all

for _,v in pairs(self.bullets) do
    v:update(dt)
end
Finally, inside Player:draw(), do this:

Code: Select all

for _,v in pairs(self.bullets) do
    v:draw()
end
As for getting the bullets to move, the simplest way would be to just update the position of the bullet during Projectile:update(dt). Something like:

Code: Select all

self.x = self.x + speed
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Can't get my bullets to work :(

Post by Ellohir »

My bad, I missed that, thanks a lot for not trusting me Marekkpie :awesome:
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Can't get my bullets to work :(

Post by Jasoco »

MarekkPie wrote:

Code: Select all

self.x = self.x + speed
Don't forget you need to multiply any movement speed by Delta Time (dt)

Code: Select all

self.x = self.x + speed * dt
Else you have bullets and other things moving way too fast every frame.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Can't get my bullets to work :(

Post by MarekkPie »

I was going for the SUPER simple, because...if you look at his code...well, you'll see.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Can't get my bullets to work :(

Post by Jasoco »

Doesn't matter. If he doesn't use dt, the game won't work properly at all. It's the most important aspect of any game.
superdajk
Prole
Posts: 3
Joined: Tue Feb 21, 2012 7:58 pm
Location: The Netherlands

Re: Can't get my bullets to work :(

Post by superdajk »

Hello everybody!

Thanks for all your help :-)! I got the bullets working like they should and even managed to get collision with the enemies working. I borrowed some code from hryx though, but I understand how that works now.

I do have another problem now, which I believe is the same as hryx's one linked above, but I can't that solution posted there to work (most likely I don't understand it). I also gave everything the delta time treatment, thanks for the heads up :-).

Anyhow, when I fire too many bullets or if there are too many enemies, the game slows down really much. Even though, as far as I know, all instances are removed properly when they should. I feel like there is something that's left behind that I haven't found, but I can't figure out what. Could somebody help?

Thanks!

Peter
Attachments
loveADP_1.love
(25.95 KiB) Downloaded 106 times
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Can't get my bullets to work :(

Post by tentus »

A few quick things.

1) love.graphics.setBackgroundColor(255, 255, 255) can be in love.load, you don't have to redo it every frame.
2) You need to put some kind of cooldown on firing bullets. Something like this:

Code: Select all

function Player:initialize(player)
	...
	self.cooldown_max = 0.25
	self.cooldown_cur = 0
end
function Player:update(dt)
	...
	if self.cooldown_cur > 0 then
		self.cooldown_cur = self.cooldown_cur - dt
	end
	if love.keyboard.isDown(self.shoot) and self.cooldown_cur <= 0 then
		self.energy = self.energy - 2
		self.bullets[#self.bullets + 1] = Projectile:new(self.num, self.direction)
		self.bullets[#self.bullets]:initialize()
		self.cooldown_cur = self.cooldown_max
	end
end
3) self.energy = self.energy + 1 should probably be before you cap it to 0/100. Also, this means they get +1 energy EVERY FRAME. Consider using dt to make it increase 1 per frame, and then just round the number off when you display it or use it for calculations.

As for the shapes not getting removed: I got nothing.
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests