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

2014/8/3

Passing a boolean value to subclass

1
2
3
4
5
danpost danpost

2014/10/26

#
1. Girl detects blob of any color and uses 'getColor' to get the specific color; 2. create a method (call it setPlatformColor) in your subclass of world with a String parameter for the color value:
public void setPlatformColor(String color)
{
    // procedure
}
you will call the method with something like '((Level)getWorld()).setPlatformColor(blob.getColor());', where blob is a reference to the blob object the girl detected (cast as a Blob object). Note: the casting is done exactly the same way that world object returned from using 'getWorld()' is cast to Level above. The '// procedure' should be a modification of what the 'activatePlatform' method in one of the previous posts does.
GreenfootStars GreenfootStars

2014/10/26

#
Gotta be honest with you, I'm a bit lost.. I'm gonna try to do this one small step at a time :)
danpost wrote...
1. Girl detects blob of any color and uses 'getColor' to get the specific color;
I am not using 'setColor' for my blobs, so I assume I can't use 'getColor' then? Right now there is nothing that differentiates between a Green and a Blue Blob except for their file name (green_blob.png vs. blue_blob.png). Blob superclass simplified:
public class SuperBlob extends Actor
{
    private String color;              //color of blob

    public SuperBlob(String color)
    {
        setImage(new GreenfootImage(color + "_blob.png")); 
    }
}
danpost danpost

2014/10/26

#
All you need to differentiate them is the 'getColor' method. It should be exactly the same as the one in the Platform class. You can change the name of the SuperBlob class to simply Blob. You also need to set the 'color' field to the value of the color parameter in the constructor:
this.color = color;
Super_Hippo Super_Hippo

2014/10/26

#
1. Yes. It can also be the other way around, but if it is in the Girl class, it will only check once per act-cycle and not once for every blob. 2. To pass the value (you used a string for that), you need to have a parameter in the world method:
//in Level class
adjustTransparency(String color) //Also call this method after creating all platforms. Just pass an empty String "" to make all Platforms transparent.
{
    for (Object obj : getObjects(Platform.class))
    {
        Platform p = (Platform) obj;
        if (color.equals(p.getColor()) p.getImage().setTransparency(255);
        else p.getImage().setTransparency(100);
    }
    //same with Activators
}
//in Girl class
Activator a = (Activator) getOneIntersectingObject(Activator.class); //or the method you use for detecting
if (a != null)
{
    Level w = (Level) getWorld();
    w.adjustTransparency(a.getColor());
}
Edit: I should have looked at the last page first...
GreenfootStars GreenfootStars

2014/10/26

#
danpost wrote...
All you need to differentiate them is the 'getColor' method. It should be exactly the same as the one in the Platform class.
I'm so sorry, I somehow didn't see that you had written that method eariler!
Super_Hippo wrote...
Edit: I should have looked at the last page first...
Your code actually helps me understand a lot :) This is from the Level class
Super_Hippo Super_Hippo

2014/10/26

#
public void adjustTransparency(String color)
I somehow missed the 'public void' part... You need the parameter color there.
GreenfootStars GreenfootStars

2014/10/26

#
Everything works beautifully! but..
Super_Hippo wrote...
//Also call this method after creating all platforms. Just pass an empty String "" to make all Platforms transparent.
As you're saying, this turns all Platforms transparent. However, I want the Red Platforms to stay solid/opaque at all times. Is this achievable?
Super_Hippo Super_Hippo

2014/10/26

#
Change the for-each to the following. This will skip the red blocks.
Platform p = (Platform) obj;  
if ("red".equals(p.getColor()) continue; //this line was added
if (color.equals(p.getColor()) p.getImage().setTransparency(255);  
else p.getImage().setTransparency(100);
GreenfootStars GreenfootStars

2014/10/26

#
Aaah "continue", sweet! Thanks!
GreenfootStars GreenfootStars

2014/10/26

#
Currently I can jump onto any paltform. I want to be able to walk through the transparent ones though. Should I do this with a boolean? For example, I have this code that checks if Mover is standing on a platform:
public boolean isOnGround()
    {
        boolean onGround = false;

        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);

        if(ground != null)
        {
            moveOutOfGround(ground);
            onGround = true;       
        }        
        return onGround;
    }
and I assume I should change if(ground != null) to something like if(ground != null && isTransparent = false) or?
Super_Hippo Super_Hippo

2014/10/26

#
Danpost suggested to just use the transparency value to check for the state of a platform instead of holding an extra field. But you can also set a Boolean in the platform class and check this one instead, if you want. This would be without:
    public boolean isOnGround()
    {
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
        if(ground != null)
        {
            if (ground.getImage().getTransparency()==255)
            {
                moveOutOfGround(ground);
                return true;
            }
        }        
        return false;
    }
GreenfootStars GreenfootStars

2014/10/26

#
I think a boolean would be better as I have to apply it to several methods. How do I call it from the Mover class?
Super_Hippo Super_Hippo

2014/10/26

#
You would do it exactly the same as above.
//instead of line 6
if (!ground.isTransparent)  //if it is public
if (!ground.getTrans()) //if it is not public, create a getter-method for the Boolean
Then you have to set the Boolean isTransparent (or however you will call it) for every platform when it is changed. Does it work as it is right now?
GreenfootStars GreenfootStars

2014/10/26

#
It works fine without the boolean yeah! What should the boolean method look like?
Super_Hippo Super_Hippo

2014/10/26

#
public boolean getTrans()
{
    return isTransparent;
}
If it works like this, then maybe you shouldn't change it. ^^
There are more replies on the next page.
1
2
3
4
5