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
Can't get my bullets to work :(
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Can't get my bullets to work :(
- Attachments
-
- loveADP_1.love
- (35.14 KiB) Downloaded 108 times
Re: Can't get my bullets to work :(
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
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 :(
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:
Now, you are adding a new bullet into the self.bullets table. Next, inside Player:update(dt), but outside any if statements, do this:
Finally, inside Player:draw(), do this:
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:
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()
Code: Select all
for _,v in pairs(self.bullets) do
v:update(dt)
end
Code: Select all
for _,v in pairs(self.bullets) do
v:draw()
end
Code: Select all
self.x = self.x + speed
Re: Can't get my bullets to work :(
My bad, I missed that, thanks a lot for not trusting me Marekkpie
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Can't get my bullets to work :(
Don't forget you need to multiply any movement speed by Delta Time (dt)MarekkPie wrote:Code: Select all
self.x = self.x + speed
Code: Select all
self.x = self.x + speed * dt
Re: Can't get my bullets to work :(
I was going for the SUPER simple, because...if you look at his code...well, you'll see.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Can't get my bullets to work :(
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 :(
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
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 120 times
- 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 :(
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: 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.
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
As for the shapes not getting removed: I got nothing.
Kurosuke needs beta testers
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 7 guests