This site requires JavaScript, please enable it in your browser!
Greenfoot back
rachpaguia@gmail.com
rachpaguia@gmail.com wrote ...

2017/3/14

DISAPPEARING OBJECT

How do you make an object disappear when pressing a key? and How do you end a game once an object reaches a specific area at the background?
danpost danpost

2017/3/14

#
rachpaguia@gmail.com wrote...
How do you make an object disappear when pressing a key?
In the act method of that object, use:
String key = "enter";
if (Greenfoot.isKeyDown(key)) getWorld().removeObject(this);
If you want the object to re-appear when the key is released, then use:
// instance field
private boolean enterDown;

// in act
if (enterDown != Greenfoot.isKeyDown("enter"))
{
    enterDown = !enterDown;
    getImage().setTransparency(enterDown ? 0 : 255);
}
How do you end a game once an object reaches a specific area at the background?
You could make that area unique by adding an actor object for the object in question to intersect with; or you can ask if the x and y of the object in question are between the required minimum and maximum value for that area. You can have the object check for anything that is unique about that area, otherwise.
Thank you so much, it worked!
You need to login to post a reply.