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

2013/3/21

Transparency Issues

SlothKing SlothKing

2013/3/21

#
I am making a maze game where the player cannot see the walls but when they touch the walls they light up with their corresponding colored walls. I am having trouble setting the transparency of all those objects to max at once. So lets say he hits a reb part of the wall, all the red parts of the wall show themselves.
danpost danpost

2013/3/22

#
How do you distinguish between one color wall and another? Are the walls to stay visible once they become visible or do they disappear again once the player is not longer touching the wall? Whatever you are doing, you probably need two fields in the wall class; one for the color (or an int for a color value) and the other for a boolean flag to determine if the color is visible or not. Let us say you are using an int field called 'colorValue' for the color and a boolean called 'visible'. Then, your code would be something like the following in the player class 'act' method or a method it calls.
Wall wall = (Wall) getOneIntersectingObject(Wall.class);
if (wall != null && !wall.visible)
{ // hit invisible wall
    int colorNum = wall.colorValue; // get wall color value
    for (Object obj : getWorld().getObjects(Wall.class))
    {  // for all walls
        wall = (Wall) obj; // type cast object to a Wall object
        // if same color wall, show wall
        if (wall.colorValue == colorNum) wall.setVisible(true)
    }
}
The above method calls the following method in the Wall class which you need to add:
public void setVisible(boolean show)
{
    visible = show;
    if (visible) getImage().setTransparency(255); else getImage().setTransparency(0);
}
SlothKing SlothKing

2013/3/22

#
I have three actor classes for a blue red and green block. when the character touches one color of block, all the blocks of the same color should appear briefly and dissapear.
danpost danpost

2013/3/22

#
You should consider combining all the wall classes into one, using an int value to determine which color image to use for that wall object. Coding would be much less complicated to do what you want to do. You can have the following:
// add this instance field
public int colorValue;

public Block(int colorNum)
{
    colorValue = colorNum;
    if (colorValue == 0) setImage("RedBlock.png");
    if (colorValue == 1) setImage("GreenBlock.png");
    if (colorValue == 2) setImage("BlueBlock.png");
}
Make the following replacements in your world constructor: -- change 'new RedBlock()' to 'new Block(0)' -- change 'new GreenBlock()' to 'new Block(1)' -- change 'new BlueBlock()' to 'new Block(2)' The names of your classes may be different, but the idea should be evident.
SlothKing SlothKing

2013/3/22

#
Alright I will do that. How would you get all of the same color ones to change transparency at the same time?
danpost danpost

2013/3/22

#
The code I gave above should then be usable.
SlothKing SlothKing

2013/3/22

#
I am having trouble getting the walls to dissapear now. I Used the code posted above, but when I start the scenario the walls stay visible and the character is not touching any of them.
SlothKing SlothKing

2013/3/22

#
Thanks for all your help so far.
danpost danpost

2013/3/22

#
Please show the Wall class code you have at this point.
SlothKing SlothKing

2013/3/22

#
public class Wall extends Actor
{
    public boolean visible;
    public int colorValue;
    /**
     * Act - do whatever the Wall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

    }

    public Wall(int colorNum)
    {   
        colorValue = colorNum;   
        if (colorValue == 0) setImage("redblock.png");   
        if (colorValue == 1) setImage("greenblock.png");    
        if (colorValue == 2) setImage("blueblock.png");   
    }

    public void setVisible(boolean show)  
    {    
        visible = show;     
        if (visible) getImage().setTransparency(255);
        else getImage().setTransparency(0);  
    }  
}
danpost danpost

2013/3/22

#
OK, you need another field to act as a timer. Refer to the following:
public class Wall extends Actor
{
    private boolean visible;
    public int colorValue;
    private int timer; // add this line

    public Wall(int colorNum)
    {   
        colorValue = colorNum;   
        if (colorValue == 0) setImage("redblock.png");   
        if (colorValue == 1) setImage("greenblock.png");    
        if (colorValue == 2) setImage("blueblock.png"); 
        setVisible(false); // add this line
    }

    public void act() 
    { // add the following code block
        if (timer > 0)
        {
            timer--;
            if (timer == 0) setVisible(false);
        }
    }

    public void setVisible(boolean show)  
    {    
        visible = show;     
        if (visible)
        {
            getImage().setTransparency(255);
            timer = 120; // add this line -- adjust as you see fit
        }
        else getImage().setTransparency(0);  
    }  
}
SlothKing SlothKing

2013/3/22

#
Thank you so much dan!
You need to login to post a reply.