The exercise is a very simple one: make 5 smileys appear in the world and have them play a sound when you click on them (each emoticon has its own sound). I added the objects by using 2 arrays; one for their images and one for their sounds. They appear just fine in the world, but when I click on them, nothing happens.
Here's my code:
public class blueWorld extends World
{
private String[] image = {"smiley1", "smiley2", "smiley3", "smiley4", "smiley5"};
private String[] sound = {"hello", "happy", "crying", "ohno", "raspberry"};
public blueWorld()
{
super(400, 100, 1);
int i = 0;
while(i < image.length)
{
Emoticon emoticon = new Emoticon(image[i] + ".png" , sound[i] + ".wav");
addObject(emoticon, 40 + 80 * i, 50);
i = i + 1;
}
}
}
public class Emoticon extends Actor
{
private String image;
private String sound;
public Emoticon(String imageName, String soundName)
{
image = imageName;
sound = soundName;
setImage(image);
}
public void act()
{
if(Greenfoot.mouseClicked(image))
{
Greenfoot.playSound(sound);
}
}
}
