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
marialo marialo

2015/8/15

#
Hello! I have this boolean method that returns checkForGreenBubble:
1
2
3
4
5
6
7
8
9
10
11
12
13
public boolean checkForGreenBubble(Color bubbleColor)
    {
        Bubble bubble = (Bubble)getOneIntersectingObject(Bubble.class);
         
        boolean checkForGreenBubble = false;
        if(bubble != null && bubbleColor == Color.GREEN )
        {
           checkForGreenBubble = true;
            
           
        }
         
      return checkForGreenBubble;
What I want to do is to use this method here:
1
2
3
4
5
6
private void changeColor(Color newColor)
    {
        if(Girl.checkForGreenBubble(Color.GREEN))
        {
            //some code
        }
Or alternatively use the returned value instead of the method:
1
2
3
4
5
6
private void changeColor(Color newColor)
    {
        if(checkForGreenBubble)
        {
            //some code
        }
But I get an error message in both cases: "non-static method cannot be referenced from a static context" in the firs case and an indefined variable in the second. What must be done to solve this? Thanks on beforehand!
Super_Hippo Super_Hippo

2015/8/15

#
The first code is in the Girl class. In what class is the other? What should change its color there? What is the reason to call the method 'checkForGreenBubble' if you just pass the green color to this method (in line 3 of the second code)?
danpost danpost

2015/8/15

#
In the first case, 'Girl' is not an actor object, but the name of a class. So, you cannot execute the 'checkForGreenBubble' method on it. It needs to be executed on an object created from that class. Specifically, in your case, on the Girl object that is in your world. In the second case, you are using 'checkForGreenBubble' as a variable, not a method. All methods have a set of round brackets after its name. Without them, the compiler assumes it to be a variable. Since there is no variable declared with that name, the 'undefined' message is given.
marialo marialo

2015/8/15

#
The method where I want to use the boolean is in a different Class (Platform). The thing is that I have to make a brick change color when the Girl intersects a third object (Bubble) Can I fix it in any possible way? Or is it completely wrong to try to execute the boolean method/ use the returned value in Platform class? Will it help to declare the variable?
Super_Hippo Super_Hippo

2015/8/15

#
In Girl class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private boolean touchedGreenBubble = false;
 
//...
 
public void act()
{
    //...
     
    if (!touchedGreenBubble)
    {
        if (getOneIntersectingObject(Bubble.class) != null)
        {
            //get a reference to the object which color you want to change and call 'changeColor' on it
        }
    }
}
In Platform class:
1
2
3
4
public void changeColor()
{
    //change the image
}
marialo marialo

2015/8/15

#
Will this work if I have two different types of Platform in one class (green and blue)? It is shown here what I have to do
Super_Hippo Super_Hippo

2015/8/15

#
Yes. Maybe this will help you:
1
2
3
4
5
6
7
for (Object obj : getWorld().getObjects(Platform.class)) //for all Platform in the game
{
    Platform p = (Platform) obj;
    if (p.type = 0) //or however you saved which color it is -- for every green color
    {
        p.changeColor(); //call the method on the object
}
danpost danpost

2015/8/15

#
Super_Hippo wrote...
1
if (p.type = 0) //or however you saved which color it is -- for every green color
'p.type = 0': should be 'p.type == 0'. 'however you saved which color it is': in the first code post 'bubbleColor' was used as a Color type field; however, it is unclear as to where this variable gets its value. Being it is not declared within the method, it would seem to be a previously declared field; but, I cannot see how a 'bubbleColor' field would be of any use within the Girl class (outside the method itself). The color of the bubble should be retrieved from the bubble object the girl object intersects.
marialo marialo

2015/8/15

#
Alright, I see. But once again how can I point out in the Girl class that the bubble object is green? And also, the variable type should indicate the color of the bubble but, as I understood it, it is present in the Girl class, right? And here again, how can I declare it? As a instance variable in the Girl class (e.g. private Color type)? Here is the code of the Bubble class if it can be of any use
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import greenfoot.*;
import java.awt.Color;
 
public class Bubble extends Mover
{
     
    private GreenfootImage[] images = new GreenfootImage[3];
    private double currentImage = 0;    
    private String mName;
    private Color mColor;
     
    /**
     * Create a Bubble.
     *
     * @param name of the Bubble.
     * @param color of the Bubble (green or blue).
     */
    public Bubble (String name, Color color)   
    {
        images[0] = new GreenfootImage(name + "1.png");
        images[1] = new GreenfootImage(name + "2.png");
        images[2] = new GreenfootImage(name + "3.png");
        setImage(images[0]);                     
        mName = name;
        mColor = color;
    }
     
    /**
     * Switch the image of the Bubble.
     */
    private void switchImage()
    {
        currentImage = currentImage + 0.1;
        if(currentImage >= images.length) {
            currentImage = 0;
        }       
        setImage(images[(int)currentImage]);
    }
     
    /**
     * Get the type of the Bubble.
     *
     * @return type of the Bubble.
     */
    public String getName()
    {
        return mName;
    }
     
    @Override
    public void act()
    {
        switchImage();       
    }
     
     
 
}
And the platform that will change the color depending on its color
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
28
29
30
31
32
33
34
import greenfoot.*;
import java.awt.Color;
 
 
    private GreenfootImage image;
    private String mName;
    private Color mColor;
     
    /**
     * Create a FunkyPlatform.
     *
     * @param name of the FunkyPlatform.
     * @param color of the FunkyPlatform.
     */
    public FunkyPlatform (String name, Color color)  
    {
        image = new GreenfootImage(name + ".png" );
 
 
        setImage(image);                     
        mName = name;
        mColor = color;
 
    }
     
     
    private void becomeTransparent()
    {
        {
            getImage().setTransparency(255);
             
        }
    }
}
Sorry for being so confused but I really have to get this task done...
danpost danpost

2015/8/15

#
marialo wrote...
how can I point out in the Girl class that the bubble object is green?
Well, you have two distinguishing values in the Bubble class to do that with -- the 'mName' and 'mColor' fields You already have a public getter method for the value of the 'mName' field (the 'getName' method) which you can make use of in the Girl class. Or, you could add a public getter method for the value of the 'mColor' field and use it.
the variable type should indicate the color of the bubble but, as I understood it, it is present in the Girl class, right? And here again, how can I declare it? As a instance variable in the Girl class (e.g. private Color type)?
There seems to be some kind of misunderstanding here. There is no need for an instance field in the Girl class to contain the color of another object unless you want to have it contain the color of the last bubble it touched (or the current solid color). This would actually be quite helpful in that it can be utilized to prevent continuously setting the platforms to their current transparencies while the girl continues to touch a bubble. Since you have 'type' as a Color type field, using a getter method for the 'mColor' field, which is of Color type, might be more advantageous.
marialo marialo

2015/8/15

#
But if I give up on this way of writing the code and just create separate classes for the green and blue platforms and bubbles and then depending on the intersection between the girl and the gree/blue bubble will try to make the platforms transparent like this: in the GreenBubble class:
1
2
3
4
5
6
7
8
9
10
11
private void makePlatformTransparent()
{ Girl girl = (Girl)getOneIntersectiongObject(Girl.class)
if(girl! = null)
{
//what should I write here in order to get the class I need, say GreenPlatform, and also get its image and set transparency on it? I've tried getWorld().getObjects(GreenPlatform.class)getImage().setTransparency(255) but getImage did not work here
}
}
 
 
 
}
danpost danpost

2015/8/15

#
marialo wrote...
But if I give up on this way of writing the code and just create separate classes for the green and blue platforms and bubbles and then depending on the intersection between the girl and the gree/blue bubble will try to make the platforms transparent
Do not give up on what you already have. Actually, it is quite workable as is. Add the getter method for the value of 'mColor' in the Bubble class and in the Girl class, when a bubble is found intersecting, do something like the following (given in pseudo-code):
1
2
3
4
5
if color of intersecting bubble is not current solid color
{
    current color = color of intersecting bubble
    update platforms
}
For updating the platforms, you need to determine what conditions should allow what actions to take place. In other words (again, in pseudo-code):
1
2
3
4
5
6
7
8
9
10
11
for all platforms
{
    if platform color is new current color
    {
        set platform image opaque (make solid)
    }
    else if platform color is not red
    {
        set platform image semi-transparent (make translucent)
    }
}
marialo marialo

2015/8/15

#
if color of intersecting bubble
current solid color
Sorry for asking again but how do I write it by using mColor? I have declared variables bubble and platform in Girl and tried to use a combination of those with the mColor but that was wrong.
danpost danpost

2015/8/15

#
marialo wrote...
Sorry for asking again but how do I write it by using mColor? I have declared variables bubble and platform in Girl and tried to use a combination of those with the mColor but that was wrong.
Those variables should only be declared from within a method in the Girl class (if at all). Going back to the 'checkForBubble' method (which should be for any color bubble and not need a parameter) in the Girl class, you should have something like this:
1
2
3
4
5
6
7
8
9
10
11
Bubble bubble = (Bubble) getOneIntersectingObject(Bubble.class);
if (bubble != null)
{
    // getting the value of 'mColor' for the intersecting bubble
    Color color = bubble.getColor();
    if (bubbleColor != color)
    {
        bubbleColor = color;
        // update platform image transparencies
    }
}
Line 1 sets up the 'bubble' variable within the method. This should work after you add the public getter method for the value of the 'mColor' field in the Bubble class.
marialo marialo

2015/8/15

#
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
There are more replies on the next page.
1
2