How Do I Make an Object Blink When It Touches Another Object? I don't want the object to just blink once, but continuously until the switch gets toggled off when another object touches said switch. I know I have to use a loop, but I don't know how/where to implement it.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Obstacle that gets switched on and off by being hit.
*/
public class Switch extends Actor
{
private boolean touched = false;
private boolean switchedOn = false;
private GreenfootSound sound = new GreenfootSound ("ping.wav");
private int i = 0;
public Switch()
{
}
public void act()
{
checkSwitch();
}
private void checkSwitch()
{
if (!touched && isTouching())
{
touched = true;
switchedOn = !switchedOn;
if (switchedOn)
{
sound.play();
setImage("block-light.png");
}
else
{
setImage("block.png");
sound.stop();
i = 0;
}
}
if (touched && !isTouching())
{
touched = false;
}
if (switchedOn && !sound.isPlaying())
{
sound.play();
}
}
private boolean isTouching()
{
return (getOneIntersectingObject(Body.class) != null);
}
}
