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

2014/11/18

Im trying to Setvolume for my sound! does not work:(

Erdrag1 Erdrag1

2014/11/18

#
im trying to set volume to my sound. this is my code in my actor class. i got an error that i had to make an setVolume method, so i did, but it does not work.
    private boolean setVolume()
    {
       if(Greenfoot.playSound("wav.wav"))
       return true;
       Greenfoot.setVolume(10);
       if(Greenfoot.stop())
       return false;
         
    }
danpost danpost

2014/11/18

#
The problem is that there is no 'setVolume' method in the Greenfoot class (nor, is there a 'stop' method in it). It (and the other) is found in the 'GreenfootSound' class as an object method. You create a GreenfootSound object and then call the 'setVolume' method on that object;
GreenfootSound gfs = new GreenfootSound("wav.wav");
gfs.setVolume(10);
gfs.play();
This snippet will create the object, set its volume and initiate the playing of the sound. The method you wrote above, I am sorry to say, does not even tell me your intent (what the method is supposed to do). The name does not really pertain to what the method appears to do. The 'if' conditions of lines 3 and 6 do not even closely resemble anything that would return a boolean (or true/false) value. What exactly did you intend that method to do?
Erdrag1 Erdrag1

2014/11/18

#
haha ;S i thought it would "if Greenfoot play sound, Setvolume , and if it stops then false, or something:P
danpost danpost

2014/11/18

#
Erdrag1 wrote...
haha ;S i thought it would "if Greenfoot play sound, Setvolume , and if it stops then false, or something:P
Or something! If there was code that was to call this method, let us say:
if (setVolume() == true)
// or
boolean volumeSet = setVolume();
I do not think either of these would really make any sense. 'setVolume' should just sets the volume; there are no questions to ask and no need for the boolean return. The Greenfoot method 'playSound' does not allow you to do anything with the sound. If you use this method to play a sound, you will not have a reference to the sound object; so you cannot run any method on it. The sound will run until it is complete played; you will not even be able to find out whether it has stopped or not.
Erdrag1 Erdrag1

2014/11/18

#
Okey i fixed it now,
  public void gameOver()
    {
        GreenfootSound gfs = new GreenfootSound("wav.wav");  
        gfs.setVolume(70);  
        gfs.play(); 
        Greenfoot.stop();

    }    
So i guess gfs is "GreenfootSounds" but i cant find anything about gfs in the Greenfoot class documentation
danpost danpost

2014/11/18

#
'gfs' is just a name I gave the variable that held the reference to the GreenfootSound object. You declare a variable by first giving the type, then by giving the name you want to call it. Just like
int x = getX();
// or
World myWorld = getWorld();
'World' and 'int' are the declared variable types and 'x' and 'myWorld' are the variable names. Once values or references to objects are associated with the variable names, you can refer to them by their names.
You need to login to post a reply.