This site requires JavaScript, please enable it in your browser!
Greenfoot back
soufou27
soufou27 wrote ...

2018/5/9

Sound won't play when mouse is clicked

soufou27 soufou27

2018/5/9

#
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);
         }
            
    }    
}
danpost danpost

2018/5/9

#
soufou27 wrote...
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. << Code Omitted >>
The mouseClicked method requires an Actor object for its parameter -- not a String object (which image is).
soufou27 soufou27

2018/5/9

#
danpost wrote...
soufou27 wrote...
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. << Code Omitted >>
The mouseClicked method requires an Actor object for its parameter -- not a String object (which image is).
Ok. The name of the actor is Emoticon, so what should I write between the brackets after mouseClicked?? "Emoticon", "Emoticon.actor", "Emoticon.class", "actor" didn't work :/
danpost danpost

2018/5/9

#
soufou27 wrote...
The name of the actor is Emoticon, so what should I write between the brackets after mouseClicked?? "Emoticon", "Emoticon.actor", "Emoticon.class", "actor" didn't work :/
The name of the actor is not Emoticon -- that is the name of the class and the Type of object your actor is. You refer to the object the code is executing for as this.
soufou27 soufou27

2018/5/9

#
danpost wrote...
soufou27 wrote...
The name of the actor is Emoticon, so what should I write between the brackets after mouseClicked?? "Emoticon", "Emoticon.actor", "Emoticon.class", "actor" didn't work :/
The name of the actor is not Emoticon -- that is the name of the class and the Type of object your actor is. You refer to the object the code is executing for as this.
Ooh now I see!! Thank you so much, it works now!
You need to login to post a reply.