Basically, I want it so when the class, "Body", intersects an object, to play a specific sound (each obstacle has a different sound)
Whenever the "Body" class intersects an obstacle, it loops the sound until the Body is NOT touching the obstacle.
How do I fix this?
private String sound;
private static boolean hit;
public Obstacle(String soundName)
{
sound = soundName;
}
public void act()
{
intersect();
}
public void intersect() {
Object body = getOneIntersectingObject(Body.class);
if (body != null && !hit) {
hit = true;
setImage("block-light.png");
Greenfoot.playSound(sound);
} else if(body == null) {
setImage("block.png");
hit = false;
}
}
