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

2017/1/12

If class is touching class

MrPancakes MrPancakes

2017/1/12

#
I have 1 class that is supposed to be changing it's image (I know how that works) after a certain object is touching another object. How can I do that?
Super_Hippo Super_Hippo

2017/1/12

#
The title talks about 'classes' and in the text you wrote 'objects'. What exactly do you want to check and change?
MrPancakes MrPancakes

2017/1/12

#
I meant that a class is touching another class and at that certain moment the image should change. Sorry for the miscommunication.
Super_Hippo Super_Hippo

2017/1/12

#
So one object of class A should change its image if it is touching an object of class B?
1
2
3
4
5
//in class A - act method or called from that
if (isTouching(B.class))
{
    setImage("image2.png");
}
If you want to change an image of the all objects of class A, you want to change the image of the object of class B or you want to change an image of objects from a class C, report back.
danpost danpost

2017/1/12

#
I think what is wanted is that if one type object touches a second type object, then all objects of a third type are to change their images.
MrPancakes wrote...
I have 1 class that is supposed to be changing it's image (I know how that works)
Specify what type objects are involved (and how they are involved) and show the code you have for changing the image for the class of objects (also indicate what class that code is located in).
MrPancakes MrPancakes

2017/1/13

#
What Danpost is trying to explain is true. I don't have any code for chinging the image yet but it will come soon. I have like a health bar that represents the time you have left. Under the background of the world, i have a ball rolling and touching some objects. If it touches the first object, let's say object B, the health bar should change it's image to another one.
danpost danpost

2017/1/13

#
So, basically, you have a bar (an object that represents an amount -- 'time', in your case) that needs to represent a new value when two objects in your world collide. You might get some pointers through my Value Display Tutorial scenario. Just run it on the site.
MrPancakes MrPancakes

2017/1/13

#
I can't run it here on my pc. Can you maybe help me with some code?
danpost danpost

2017/1/13

#
MrPancakes wrote...
I can't run it here on my pc. Can you maybe help me with some code?
I can tell you that you will need at least four things:
  • a reference field for the actor that displays the bar
  • a control field for the value the bar is to display
  • a method to update the image of the bar both initially and when the control value changes
  • a method to change the control value that calls the method in the last item listed
These should go in, I would think (since it is a timer bar, sort of), in your World subclass.
You need to login to post a reply.