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

2021/11/30

Change image of an actor from another class

salim5112 salim5112

2021/11/30

#
Hello, I have 2 actors, actor1 and actor2, when actor1 touches actor2, i want the image of actor2 to change, i did that by writing in the class of actor2: if(isTouching(actor1.class)) { setImage("image"); } but now i want to the equivalent of this code but written in the class of actor1, is that possible? Thanks for reading.
danpost danpost

2021/11/30

#
salim5112 wrote...
I have 2 actors, actor1 and actor2, when actor1 touches actor2, i want the image of actor2 to change, i did that by writing in the class of actor2: << Code Omitted >> but now i want to the equivalent of this code but written in the class of actor1, is that possible?
That should not be a problem, provided that you do not make them not touch when found touching to begin with.
salim5112 salim5112

2021/11/30

#
If i write that same code in the actor1 class it's the actor1 that's gonna change, but i want the actor2 to change its image. on the class of actor1: if (isTouching(actor2.class) { actor2.setImage("image"); // i need the equivalent of this statement if its possible }
danpost danpost

2021/11/30

#
salim5112 wrote...
If i write that same code in the actor1 class it's the actor1 that's gonna change, but i want the actor2 to change its image. on the class of actor1: if (isTouching(actor2.class) { actor2.setImage("image"); // i need the equivalent of this statement if its possible }
if (isTouching(actor2.class)) {
    Actor a2 = getOneIntersectingObject(actor2.class);
    a2.setImage("image");
}
or
if (isTouching(actor2.class)) getOneIntersectingObject(actor2.class).setImage("image");
or
Actor a2 = getOneIntersectingObject(actor2.class);
if (a2 != null) a2.setImage("image");
salim5112 salim5112

2021/11/30

#
thanks a lot
danpost danpost

2021/11/30

#
salim5112 wrote...
thanks a lot
The last way is probably best as only one time is intersection checked, there.
You need to login to post a reply.