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

2015/8/15

Calling a method from another class

1
2
danpost danpost

2015/8/15

#
marialo wrote...
This time I get an error when I write setImage(image) since "image has private access in greenfoot.Actor". I made the instance variable GreenfootImage image public for the platform but it did not help
You need to show the actual code where around which that error took place and also inform which class the code resides in. You should also explain what you are attempting to do with that block of code. You should also note that if the girl is checking for bubble objects, then the bubble objects should NOT be checking for the girl object. I was not sure if you had both or not; but, I thought that it should be mentioned anyway.
marialo marialo

2015/8/15

#
I have it in the Girl class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private void checkForBubble(Color bubbleColor, Color platformColor)
    {
        Bubble bubble = (Bubble)getOneIntersectingObject(Bubble.class);
 
       
         
        if (bubble != null)
        {
            Color color = bubble.getColor();
            if(bubbleColor != color)
            {
                bubbleColor = color;
                 
                for ( Object obj: getWorld().getObjects(Platform.class))
                 
                {if(platformColor == color)
                    {
                        setImage(image).setTransparency(255);}
 
                    else if(platformColor = Color.RED)
                    {
                        setImage(image).setTransparency(50);
                    }
                }
            }
        }
    }
And image is defined in the platform class
1
2
3
4
5
6
7
8
9
10
11
12
{
public GreenfootImage image;
//...
 
public Platform (String name, Color name)
{
image = new GreenfootImage (name + ".png");
setImage(image);
mName = name;
mColor = color;
}
}
danpost danpost

2015/8/15

#
First off, the 'checkForBubble' method should not have any parameters. It should start:
1
private void checkForBubble()
The 'bubbleColor' field should be declared outside the method:
1
private Color bubbleColor;
The 'platformColor' variable should be declared inside the method within the 'for' loop (before the first 'if' statement):
1
2
Platform platform = (Platform)obj;
Color platformColor = platform.getColor();
Finally, your lines 18 and 23 above are attempting to work with the image of the girl object -- not the platform. Also, you are using 'setImage', which is not what you want to do. You are not changing the image of the platform, just changing the transparency of the current image.
1
2
3
GreenfootImage platformImage = platform.getImage();
// followed later with, for example
platformImage.setTransparency(200);
unless you want to call 'platform.setTransparent();' or something like that. There is really no need for the 'image' field in the Platform class. Once the image is set, the only change is to its transparency; the image itself is still the same image.
marialo marialo

2015/8/16

#
Thank you so much!!!! I can't believe it works!!!
You need to login to post a reply.
1
2