| Author |
Message |
Kevin McAllister
Posts: 37
|
Posted: Fri Jul 07, 2006 11:07 pm Post subject: please java what |
|
lists and java and c are horrible yuck why cant everything just be like haskell?
so lets say I have a 'bullet' object, which holds information about its x and y positions and velocities. The bullet objects have an 'update' method defined on them, which when invoked just changes the x and y positions according to the bullet's x and y velocities. Now I could keep all the bullets in a list, and on every step run through the list with a for loop, updating each bullet as I go. BUT lets say a bullet exits the screen. Then obviously I want to destroy it. So in the for loop I might say (in psuedo code) "if xpos < 0 || xpos > width then bullet = null".
But then the next time that I traverse the list, my program dies because I try to call a method on the null bullet.
So I can see a bunch possible solutions:
Define the 'bullet' methods to work for null objects..but I dont know how to do this.
In the for loop that traverses the list, do something like
" If bullet = null then just move onto the next ball"
But I dont know how to do this either.
EDIT : THIS IS THE WAY I WANT TO DO IT PLEASE TELL ME IT IS POSSIBLE
Find some other way of updating all the balls apart form putting them in a list. Is there some way of saying "Update all the balls that exist" ? |
|
| Back to top |
|
 |
!=
Posts: 163
|
Posted: Fri Jul 07, 2006 11:29 pm Post subject: |
|
Why not build a new list at the same time as you process it, removing the bullets that are not needed anymore, because they are dead?
Switching both lists when you exit the loop.
Otherwise there is ListIterator if you really want to know |
|
| Back to top |
|
 |
Monthenor
Posts: 455
|
Posted: Fri Jul 07, 2006 11:53 pm Post subject: |
|
I solved this with a TimerGod maintaining a list of TimerTasks, and when the TimerTasks complete they call their cancel() method, which calls the static method TimerGod.cancel(this) to remove itself.
That is a horrible way to do it! God what have I done?
Just have your TimerGod remove null objects when it finds them.
for(int i = 0; i < list.size(); i++)
{
if(list.elementAt(i) == null)
{
list.remove(i);
i--;
}
else
{
((Bullet)list.elementAt(i)).doShit();
}
} |
|
| Back to top |
|
 |
Takashi
Posts: 820
|
Posted: Sat Jul 08, 2006 4:43 am Post subject: |
|
As said previously, if's it's Java you want ListIterator.
Otherwise you need to build a linked list that can relink his own chains. |
|
| Back to top |
|
 |
chevluh
Posts: 183
|
Posted: Sat Jul 08, 2006 7:46 am Post subject: |
|
You need to first remove the bullet from the list (using the method that is most certainly present in the iterator, that will take care of remaking the links) and THEN null the bullet. In fact, in you're in java, removing the bullet from the list should nullify it (but it doesn't work the other way round!).
It's the "then bullet=null" from your original reasoning that's the problem. You wanna use a list, you've gotta do it by the book, because when used correctly (ie with the provided methods and iterators, ie "listIterator.remove()") it's precisely engineered to avoid that kind of problems. Nullifying doesnt remove the actual list node, it just invalidates it. If you spawned 10000 bullets then nullified all of them you'd have a lift with 10000 invalid nodes instead of an empty list, which is silly. |
|
| Back to top |
|
 |
Krabjuice
Posts: 114
|
Posted: Sat Jul 08, 2006 8:02 am Post subject: |
|
| I simply make an indication of which bullets are visible and which bullets aren't. An boolean vector should do the trick, or something. |
|
| Back to top |
|
 |
janus
Posts: 63
|
Posted: Sun Jul 09, 2006 4:33 am Post subject: |
|
| The traditional method when iterating over a list you can't modify is to build a 'kill list' every update and at the end of the update, iterate through the kill list and remove every item from the main list that exists in the kill list. |
|
| Back to top |
|
 |
Kevin McAllister
Posts: 37
|
Posted: Mon Jul 10, 2006 8:19 pm Post subject: |
|
ok lets say I want to be able to press q to kill all the bullets
| : |
if (keyPressed && (key=='q'))
{for (Iterator it=balls.iterator(); it.hasNext();)
{it.remove();}} |
this does kill all the bullets, but it also gives me an IllegalStateException.
The for loop seems right though. While there is a next element, kill the next element. So whats the deal. |
|
| Back to top |
|
 |
Takashi
Posts: 820
|
Posted: Tue Jul 11, 2006 3:20 am Post subject: |
|
You can only call remove once for per next call, it's written pretty clearly in the Java API.
This will provably work:
| : |
if (keyPressed && (key=='q'))
{
for (Iterator it=balls.iterator(); it.hasNext();)
{
it.next();
it.remove();
}
} |
|
|
| Back to top |
|
 |
Kevin McAllister
Posts: 37
|
Posted: Thu Jul 13, 2006 4:35 am Post subject: |
|
ok right. i guess this will eventually turn into some sort of bullet pattern. mouse click to accelerate the particles toward the cursor. hidden secret: I want to make a shooter with gravity based gameplay. press s to freeze the particles.
www.members.optusnet.com.au/akaash |
|
| Back to top |
|
 |