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

2014/7/2

Adding sound: playing different sounds based on image clicked

blangley83 blangley83

2014/7/2

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Emoticon here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Emoticon extends Actor
{
    /**
     *INSERT CODE BELOW
     *    Create two instance varaibles of type String
     *         one named sound
     *         one named image
     */
private String sound;
private String image;
private String[] images ={"smiley1","smiley2","smiley3","smiley4","smiley5"};
private String[] sounds = {"hello","happy","crying","ohno","raspberry"};
 
    /**
     * INSERT CODE BELOW
     *   Write a construtor method
     *         The method should have two parameters
     *                type:  String     name:   newImage
     *                type:  String     name:   newSound
     *
     *         The method should
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter
     *
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image.
     *
     */
    public Emoticon(String newImage, String newSound)
    {
        image=newImage;
        sound=newSound;
        setImage(image);
    }
    /**
     * play a sound when the mouse is clicked in the emotion image
     */
    public void act()
    {
        // When the mouse is click on this object, play the sound.
         if(Greenfoot.mouseClicked(this))
         {
             /**
              * INSERT CODE BELOW
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound
              *
              */
             Greenfoot.playSound(sound);
         }
 
    }
}
I have been working on these for so long. Please help. I am taking 5+ hours today, and 3+ yesterday. It is starting to click but this is just stumping me. Ive tried adding arrays to the emoticon class, someone in reddit suggested something to check if an array has been clicked? I know the solution cannot be that detailed as we have not gotten far in the text, please please please help!! thank you!!
danpost danpost

2014/7/2

#
The arrays do not go into the Emoticon class. The String values, for each Emoticon object, from the elements of the arrays, are passed to the Emoticon class as each Emoticon object is being created from within your subclass of World. Are you learning loops, per chance? or learning how to work with arrays? No matter. Either way you should refer to Variables - Arrays for arrays and Control Flow Statements - The for statement for the 'for' loop.
blangley83 blangley83

2014/7/2

#
Yes, we have just learned loops. I just cannot think of how to do this. Why wont putting in
1
Greenfoot.playSound(sounds[])
work?
davmac davmac

2014/7/2

#
Why wont putting in (...) work?
Because the playSound method requires a String, not a String array. Putting it another way, you are saying "play this sound" but then providing a whole bunch of sounds.
I just cannot think of how to do this.
As danpost says, the arrays do not go into the Emoticon class. Where are you actually creating the Emoticon instances (ie where do you have "new Emoticon( ... )")? show us the code in that class.
blangley83 blangley83

2014/7/2

#
I have that in my HomeworkWorld class. I believe this is the code you are referencing to? If you would like, I can post the entire HomeworkWorld class
1
2
3
4
5
for(int i=0; i<5; i++) {
        images[i]=new String("smiley"+(i+1)+".png");
        sounds[i]=new String("sound"+(i+1)+".wav");
        addObject(new Emoticon(images[i], sound[i]), 70*(i+1),50);
    }
danpost danpost

2014/7/2

#
Line 3 is changing the String values in the 'sounds' array to "sound1.wav", "sound2.wav", etc. instead of "hello.wav", "happy.wav", etc. Obviously "sound1.wav", et. al., will not be found.
blangley83 blangley83

2014/7/2

#
I THINK I DID IT !!!! FINALLY!!!!!!!!
Lboogie0208 Lboogie0208

2014/7/4

#
I am having a problem with this too can someone please help. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HomeworkWorld here. * * @author () * @version (June 3, 2014) */ public class HomeworkWorld extends World { /** * INSERT CODE BEOLW * Add two instance variables that are of type String * one will be an array of images * one will be an array of sounds * DO NOT INCLUDE THE FILE SUFFIX - add that through code later * * Note - each image is 55x55 */ private String image = {"smiley1", "smiley2", "smiley3", "smiley4", "smiley5"};; private String soundfile = {"hello", "happy", "crying", "ohno", "raspberry"}; /** * Constructor for objects of class HomeworkWorld. * */ public HomeworkWorld() { // Create a new world with 400x100 cells with a cell size of 1x1 pixels. super(400, 100, 1); } /** * INSERT CODE BELOW * Write a "for" loop * It should loop 5 times (you can either hard code 5 or use code) * It should use the method addObject( Actor object, int x, int y) to add new Emoticons to your world. * Note: concatinate the ".png" for images and ".wav" for the sounds * * Determine the x value using a mathematical calculation * The y value can be either hard coded or calculated * */ private void makeImages() { //make the Emoticon Images for(int i = 0; i<5; i++) { images=new String("smiley1", "smiley2", "smiley3", "smiley4", "smiley5"); sounds=new String("hello","happy","crying","ohno","raspberry"); addObject(new Emoticon(images, sound), 70*(i+1), 50); } } }
Lboogie0208 Lboogie0208

2014/7/4

#
This is what i have for the Emoticon import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Emoticon here. * * @author (your name) * @version (a version number or a date) */ public class Emoticon extends Actor { /** *INSERT CODE BELOW * Create two instance varaibles of type String * one named sound * one named image */ private String sound; private String image; { /** * INSERT CODE BELOW * Write a construtor method * The method should have two parameters * type: String name: newImage * type: String name: newSound * * The method should * 1. set the instance variable image to the value passed by the corresponding parameter * 2. set the instance variable sound to the value passed by the corresponding parameter * * 3. Use the method setImage( java.lang.String filename ) to set the image. * */ Emoticon emoticon = new Emoticon(image,sound); image = newImage; sound = newSound; setImage(image); } /** * Act - do whatever the Emoticon wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // When the mouse is click on this object, play the sound. if(Greenfoot.mouseClicked(this)) { /** * INSERT CODE BELOW * Use the method Greenfoot.playSound( java.lang.String filename ) to play the sound * */ Greenfoot.playSound(sound + ".wav"); } } }
danpost danpost

2014/7/4

#
@Lboogie0208 -- please start a new discussion thread for your issue and use the 'code'tag below the input box to insert your code into your post. Having the ability to refer to line numbers would be a great help in being able to assist. NVM -- I see that you have started a new one.
You need to login to post a reply.