Question about adding bullets to arraylists
A few days ago, I asked a question about rendering multiple bullets to the screen and a lot of you suggested I use ArrayLists. I tried a bunch of stuff, but that didn't exactly work (Note: I DO have prior knowledge of arraylists, so I'm not completely clueless XD).
What exactly is the FORMATTING for making it work? I know there isn't a single method of doing it, but I would like some kind of sample which demonstrates an efficient technique for adding the bullet object to the arraylist, creating a rectangle at the player's coords, etc.
I promise I'm not being lazy and asking for people to do it for me XD I tried all that I could but I ended up with the bullet restarting its position at the player's position whenever I press the trigger button, rather than a NEW bullet being created. I do know for a fact that my arraylist IS being updated, because I print out the size of it whenever I press the trigger button, and it updates.
I know this is vague, but a vague answer is kind of what I need. I just need the concept, or the formatting for how I would go about doing it.
Tl;dr : ArrayList being updated, but same bullet being reused.
Thanks to whomever replies! Means a lot :D
If you have problems with your code, post your code.
Don't make us guess when you can provide information which will help us answer your questions.
By the sounds of things, you almost had this working -- I would suggest showing your code where you attempted to create a new bullet upon pressing the trigger button, and potentially also show where & how the bullets are being drawn and updated.
Also, this could have been a part of your earlier thread; there's not really a reason to create a new thread to continue the topic when the previous thread is still high up on the first page.
Hello to all my stalkers.
If you have problems with your code, post your code.
Don't make us guess when you can provide information which will help us answer your questions.
By the sounds of things, you almost had this working -- I would suggest showing your code where you attempted to create a new bullet upon pressing the trigger button, and potentially also show where & how the bullets are being drawn and updated.
Also, this could have been a part of your earlier thread; there's not really a reason to create a new thread to continue the topic when the previous thread is still high up on the first page.
Oops, I'll go do that. Sorry!
I will hazard a wild guess that maybe you're doing things such that when you create the new bullet and set its position near the player, you end up setting the position of the previously fired bullet to the same location.
I will hazard a wild guess that maybe you're doing things such that when you create the new bullet and set its position near the player, you end up setting the position of the previously fired bullet to the same location.
How would I avoid doing that? It sounds logical but I'm not sure how I would be doing that...
First, trace through your code and verify if that's what's actually happening. If you aren't aware, you can add some code like the following somewhere near your paint routine and place a breakpoint.
if (bulletsFiredCount >= 2 && spriteToPaint == bullet)
{
int asdf = 1; //Place a break point here.
}
You want it somewhere that you can inspect the destination of the bullet sprite about to be painted. Check the destination of both bullets and if they're the same then you want to make sure that each bullet has its own separate set of current co-ordinates and that they're each used when painting the bullets.
It sounds like your design is flawed. I may be misunderstanding your question, but where is the confusion? When you "pull the trigger", simply add another bullet object to your draw list(how ever you manage that) at pos x,y,z and go. Where is the confusion? How could you possibly be reusing the same bullet?
Are you sure you are making a new item and placing it in the array? The old thing don't just automagically appear near the user you are doing something to cause that to be drawn
at the players location
You've been asking a lot of questions lately, which is really what this forum is for, but it doesn't seem like you're figuring much out without our help. If you're going to be any sort of programmer (or any sort of anything, really), you need to figure out how to figure some of this out for yourself.
It sort of doesn't really sound like you have a good enough understanding of programming in general to start making games. Even simple games (pong) are fairly complex compared to what a lot of for-beginners tutorials and books teach you. It might be worth at least having a general understanding of how to solve these problems on your own: using documentation, for example, to at least make sure that you're using your tools correctly before you ask others to figure your code out for you.
We've all been beginners, and all of us are beginners at something. You're not going to improve, though, if we keep finding your bugs for you.
Inspiration from my tea:
"Never wish life were easier. Wish that you were better" -Jim Rohn
soundcloud.com/herwrathmustbedragons
Also, it's not really good forum etiquette to remove your questions and replace them with "[solved]". That way no-one else can benefit from the question and answer later on. When your issue is solved, reply to the thread letting other people know what the problem was. Then try to learn from the experience.