Sign in to follow this  
null;

Question about adding bullets to arraylists

Recommended Posts

So the last few days I've just been kind of polishing my game - the reason I haven't really focused on mechanics and stuff is because I couldn't get past a certain problem.

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 Edited by Josh Petrie
Restored post body from history.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Apparently I shouldn't have even had that problem in the first place according to a few (not in a mean way at all, but it would make sense). With that rationale, why would people benefit off of a problem regarded as being 'dumb'? (My words, not anyone else's).

If my post is being down voted to oblivion because of that, here's my fix:

I screwed up my draw method. It was being called in a weird way.

Also, I've made pong. I'm confident in my abilities and look at documentation. It was a simple mistake.

Share this post


Link to post
Share on other sites

 

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.


Apparently I shouldn't have even had that problem in the first place according to a few (not in a mean way at all, but it would make sense). With that rationale, why would people benefit off of a problem regarded as being 'dumb'? (My words, not anyone else's).

If my post is being down voted to oblivion because of that, here's my fix:

I screwed up my draw method. It was being called in a weird way.

 

 

Because, regardless of whether if your problem was dumb or not, other people also make mistakes.

I still makes stupid mistakes all the time, as do all other people, I would guess. There's nothing special about making stupid mistakes.

 

Mainly, the thing that improves with experience is your ability to troubleshoot and find the error on your own (and the ability to create some really outstanding mistakes inconceivable by most beginners).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this