This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jacob_Chapman
Jacob_Chapman wrote ...

2018/4/30

Two Specific Actors Remove Eachother

Jacob_Chapman Jacob_Chapman

2018/4/30

#
I'm currently having issues with trying to have two actors erase each other. My problem comes from both actors being randomly generated from 2 classes, and having both specific actors be deleted. The world is set up as a 9x9 grid where two rows of randomly generated actors, of types A, B, and C, move towards each other. Each side is split into A1, B1, and C1 (Left), and A2, B2, and C2 (Right). When A and B meet, A destroys B. When B and C meet, B destroys C. And when C and A meet, C destroys A. When A and A/B and B/C and C meet, they are supposed to destroy each other. But no matter what I try, only one of the two gets destroyed. I've tried moving both off screen, having both erase themselves, etc. I was thinking that I could set a variable (dead) on each of them to true, and have the World check for all actors with said variable set to true and erase them each time act() is called, but I'm not sure how to do that. Thoughts?
Jacob_Chapman Jacob_Chapman

2018/4/30

#
What I really need to know, is if two randomly generated actors of two different classes collide, how can I get them to delete specifically the actor they collide with and themselves? If they delete one and then the other, I get a nullpoint because the condition of them touching is no longer met. They need to be deleted simultaneously. How can I do that?
danpost danpost

2018/4/30

#
Jacob_Chapman wrote...
I was thinking that I could set a variable (dead) on each of them to true, and have the World check for all actors with said variable set to true and erase them each time act() is called, but I'm not sure how to do that.
That would be totally unnecessary as you should be able to get it to work in the other ways you tried.
I've tried moving both off screen, having both erase themselves, etc.
Show your attempts and explain what made them insufficient. Give exact details. If error messages, copy/paste them here. The more info you provide, usually the quicker the issue can be resolved.
Jacob_Chapman Jacob_Chapman

2018/5/2

#
Method 1
1
2
getWorld().removeObject(A2.class);
getWorld().removeObject(this);
Result : Unsure of which instance of A2 to remove if multiple have spawned and are being removed. Method 2
1
2
removeTouching(A2.class);
getWorld().removeObject(this);
Result : Illegal State Exception, After removing the instance of A2 that is being touched, Actor is no longer in world, so A1 no longer meets the condition to remove itself. Method 3
1
2
3
4
//A1
removeTouching(A2.class);
//A2
removeTouching(A1.class);
Result : One actor deletes the other first, resulting in the Illegal State Exception from above. Method 4
1
2
//On both A1 and A2
getWorld().removeObject(this);
Result : One actor deletes itself first, once again causing the Illegal State Exception. Method 5
1
setLocation(9, 9);
Result : One actor is warped before the other, so only one is removed from the field. Method 6 I'd like to set both actors to either a boolean state of true to an int state of 1 to determine that they are dead, and have the world clean them up. But as stated above, I do not know how to achieve this. I appreciate the help.
danpost danpost

2018/5/2

#
Method 2 looks like the correct way -- and I presume that you tried that in the A1 class. If you are getting an IllegalStateException error there, then it is probably because of some code that executed earlier during the act step of that A1 object.
Jacob_Chapman Jacob_Chapman

2018/5/4

#
danpost wrote...
Method 2 looks like the correct way -- and I presume that you tried that in the A1 class. If you are getting an IllegalStateException error there, then it is probably because of some code that executed earlier during the act step of that A1 object.
The problem comes from the full code.
1
2
3
4
if(isTouching(A2.class)) {
removeTouching(A2.class);
getWorld().removeObject(this);
}
First, A2 is removed. Then, there is an illegal state, because A1 is no longer touching A2, so the condition to erase itself is no longer met.
danpost danpost

2018/5/4

#
Jacob_Chapman wrote...
The problem comes from the full code.
1
2
3
4
if(isTouching(A2.class)) {
removeTouching(A2.class);
getWorld().removeObject(this);
}
First, A2 is removed. Then, there is an illegal state, because A1 is no longer touching A2, so the condition to erase itself is no longer met.
Line 3, in itself, does not rely on the two objects touching. You are not correct as to why you are getting the error. There is some broader context which you have yet to provide. Post the code of the entire class for review.
You need to login to post a reply.