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

2016/2/8

How do you make something visible forever after being clicked?

bobbyjones2000 bobbyjones2000

2016/2/8

#
Hello Greenfoot community, I'm new to Greenfoot and have done some of the tutorials, and have tried to make something on my own. It is something I'm making so I know how to make a tic-tac-toe game later on. I want to be able to click a house that is invisible to begin with and make it visible for the rest of the run time. However, it only turns What is wrong with my code so far?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class House here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class House extends Actor
{
    /**
     * Act - do whatever the House wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     
        getImage().setTransparency(0);
       if (Greenfoot.mouseClicked(this))
     {
      
      getImage().setTransparency(100);
    }    
}
}
Thank you.
danpost danpost

2016/2/8

#
Every act cycles, line 18 will reset the transparency back to zero and only if it was clicked on that cycle will it show. However, it will show for only about 0.02 seconds, which may preclude it from actually being visible at all since the screen may not be refreshed before becoming transparent again. Basically, you need to set the initial transparency before the act method is called on the House object. That mean that you need to do it either when the House object itself is created or at the time when it is being added into the world; which in turn means that you code it either in the constructor of the class or in the 'addedToWorld' method. Only lines 19 through 23 should be in the 'act' method.
bobbyjones2000 bobbyjones2000

2016/2/18

#
How would I set the transparency when the House is created?
danpost danpost

2016/2/18

#
bobbyjones2000 wrote...
How would I set the transparency when the House is created?
Just place the code to set the initial transparency in the constructor of the class.
You need to login to post a reply.