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

2014/4/9

Making my piano switch sounds after clicking the button

Noviij Noviij

2014/4/9

#
This deals with loops. I need help making my sound play when I click the button. Its 3 different sounds altogether. The default is piano. When i click the button it switches to bassoon and the one i created is guitar but when i click it the sounds goes back to the piano. Any help?
danpost danpost

2014/4/9

#
We will need to see how you set up the sounds and how you are trying to switch between them. Use the 'code' link below the 'Post a reply' box to insert your code.
Noviij Noviij

2014/4/9

#
This is the code for World inc
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. Kolling
 * @version: 0.1
 */
public class Piano extends World
{
    private Key[] keys = new Key[12];

    private String[] whiteKeys = {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "\\"};

    private String[] whiteNotes = {"2c", "2d", "2e", "2f", "2g", "3a", "3b", "3c", "3d", "3e", "3f", "3g"};
    
    private String[] prefix = {"Piano\\", "Bassoon\\", "Guitar\\"};
    private String postfix = ".wav";

    /**
     * Make the piano.
     */
    public Piano() 
    {
        super(800, 400, 1);
        
        makeKeys();
       
        addObject(new Button(this), 400, 340);
    }

    public void makeKeys(){
        int i = 0;
        while (i < 12){
            Key key = new Key(whiteKeys[i], prefix[0] + whiteNotes[i] + postfix);
            addObject(key, 54+i*63, 140);
            keys[i] = key;
            i++;
        }
    }
    
    // instrument: 0 -- Piano
    //             1 -- Bassoon
    
    public void changeSoundFilesForKeys(int instrument){
        if (instrument == 0){// 0: Piano
            int i = 0;
            while (i < 12){
                keys[i].changeSoundFile(prefix[0] + whiteNotes[i] + postfix);
                i++;
            }
        }
        else { // 1: Bassoon
            int i = 0;
            while (i < 12){
                keys[i].changeSoundFile(prefix[1] + whiteNotes[i] + postfix);
                i++;
                
                
                
//                 while (i < 12) {
//                      keys[i].changeSoundFile(prefix[2] + whiteNotes[i] + postfix);
//                 i++;
//                 
            }
        }
    }
}
Noviij Noviij

2014/4/9

#
This is code for button that is used to switch instruments
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PianoButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button  extends Actor
{
    private GreenfootImage[] images = new GreenfootImage[3];    

    //private GreenfootImage imageUnclicked;
    //private GreenfootImage imageclicked;
    private Piano myPiano;
    
    public Button(Piano piano){
        images[0] = new GreenfootImage("PianoButton.png");
        images[1] = new GreenfootImage("BassoonButton.png");
        images[2] = new GreenfootImage("GuitarButton.png");
//        
        setImage(images[0]);
        myPiano = piano;
    }

    public void act() 
    {
        // The Cruise button will engage when being pressed
        if(Greenfoot.mouseClicked(this)){
            if (getImage() == images[0]){
                setImage(images[1]);
                // change to Bassoon
                // 0: piano
                myPiano.changeSoundFilesForKeys(1);
                
            }
            else {
                setImage(images[0]);
                setImage(images[2]);// change to CS Courses
                myPiano.changeSoundFilesForKeys(0);
            }
        }
    }       
}
danpost danpost

2014/4/9

#
In your Button class, line 37 needs to be an 'else-if', not just an 'else'. Then you can add the code for the third instrument type. There is a better way to do this, however. Add an instance 'int' field to the Button class for the current image used. Then, you can just rotate it between the valid values and use it as the index in the array.
import greenfoot.*;

public class Button  extends Actor
{
    private GreenfootImage[] images =
    {
        new GreenfootImage("PianoButton.png"),
        new GreenfootImage("BassoonButton.png"),
        new GreenfootImage("GuitarButton.png")
    };
    private Piano myPiano;
    private int index;
    
    public Button(Piano piano)
    {
        setImage(images[index]);
        myPiano = piano;
    }

    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            index = (index+1)%images.length;
            setImage(images[index]);
            myPiano.changeSoundFilesForKeys(index);
        }
    }       
}
Then, in your world class: why not use the 'instrument' argument in the 'changeSoundFilesForKeys' method instead of hard-coding the '0', '1', and '2' for the indecis of the array.
public void changeSoundFilesForKeys(int instrument)
{
    int i = 0;
    while (i < 12)
    {
        keys[i].changeSoundFile(prefix[instrument] + whiteNotes[i] + postfix);
        i++;
    }
}
Noviij Noviij

2014/4/9

#
Ok I got the sound to play when it switches by changing the 1 to 2. I still don't understand how to make it continue to cycle through the instruments. When i click the guitar class it doesn't return to piano.
 else {
                setImage(images[0]);
                setImage(images[2]);// change to CS Courses
                myPiano.changeSoundFilesForKeys(2);
danpost danpost

2014/4/9

#
It appears you are still using 'else' and not 'else if'. You would use a similar condition check to line 30 of the class at line 37. Then add the final 'else' for the third instrument.
// Piano to Bassoon
if (getImage() == images[0]){
    setImage(images[1]);
    myPiano.changeSoundFilesForKeys(1);
}
// Bassoon to Guitar
else if (getImage() == image[1]){
    setImage(images[2]);
    myPiano.changeSoundFilesForKeys(2);
}
// Guitar to Piano
else {
    setImage(image[0];
    myPiano.changeSoundFilesForKeys(0);
}
Noviij Noviij

2014/4/9

#
That worked. I was putting "{" instead of the "(" so it wouldn't compile. Thank you very much!!!
You need to login to post a reply.