Page 1 of 1
Can't get my bullets to work :(
Posted: Tue Feb 21, 2012 8:17 pm
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
Re: Can't get my bullets to work :(
Posted: Tue Feb 21, 2012 10:10 pm
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
Re: Can't get my bullets to work :(
Posted: Tue Feb 21, 2012 10:35 pm
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:
Re: Can't get my bullets to work :(
Posted: Tue Feb 21, 2012 11:53 pm
by Ellohir
My bad, I missed that, thanks a lot for not trusting me Marekkpie
Re: Can't get my bullets to work :(
Posted: Thu Feb 23, 2012 6:16 am
by Jasoco
Don't forget you need to multiply any movement speed by Delta Time (dt)
Else you have bullets and other things moving way too fast every frame.
Re: Can't get my bullets to work :(
Posted: Thu Feb 23, 2012 6:49 am
by MarekkPie
I was going for the SUPER simple, because...if you look at his code...well, you'll see.
Re: Can't get my bullets to work :(
Posted: Thu Feb 23, 2012 7:27 am
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.
Re: Can't get my bullets to work :(
Posted: Fri Feb 24, 2012 12:50 pm
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
Re: Can't get my bullets to work :(
Posted: Fri Feb 24, 2012 3:12 pm
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.