how would you make the block blink (toggle) when you have a body hit them. I got the block to turn on and off if an object hits them, but i am required to make it so when a body hits a block it flashes on and off and also plays a sound periodically.
view plaincopy to clipboardprint?
public class Obstacle extends Actor
{
private String sound;
private boolean touched = false;
private boolean toggledOn = false;
/**
* Create an obstacle with an associated sound file.
*/
public Obstacle(String soundFile)
{
sound = soundFile;
}
/**
* Each act cycle, check whether we were hit. If we were, play our sound.
*/
public void act()
{
checkToggle();
}
private void checkToggle()
{
if (!touched && isTouching())
{
touched = true;
toggledOn = !toggledOn;
if (toggledOn)
{
setImage ("block-light.png");
Greenfoot.playSound(sound);
}
else
{
setImage ("block.png");
}
}
if (touched && !isTouching()) touched = false;
}
public void playSound()
{
Greenfoot.playSound(sound);
}
private boolean isTouching()
{
return (getOneIntersectingObject(Body.class) !=null);
}
}

