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

2018/4/8

Greenfoot Chapter 10 MP3 Player

t_o98 t_o98

2018/4/8

#
I'm completely stuck on how to implement the volume up and down buttons to change the volume of the song. So far I have been able to decrease the volume, but the variable I have for volume is not being updated for both classes? I'm sure it's a simple fix, but I am lost and could use some advice.
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
public class MyWorld extends World
{
    GreenfootSound music = new GreenfootSound("freebird.mp3");
    private GreenfootImage volumeB = new GreenfootImage(50, 60);
    private int volume = 100;
   
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld(){   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setButtons();
    }
     
    public int getVolume(){
        return volume;
    }
     
    public void setTheVolume(int volume){
       this.volume = volume;
    }
     
    public void setButtons(){
         
        startButton start = new startButton(music);
        addObject(start, 300, 200);
         
        volumeDown olumeDown = new volumeDown(music, volume);
        addObject(olumeDown, 70, 300);
         
        volumeUp olumeUp = new volumeUp(music, volume);
        addObject(olumeUp, 70, 250);
         
        
    }
     
}
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
public class volumeDown extends Actor
{
    private GreenfootSound music;
    private int volume;
     
    public volumeDown(){}
     
    public volumeDown(GreenfootSound music, int vol){
        this.music = music;
        this.volume = vol;
    }
     
    /**
     * Act - do whatever the volumeDown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        volumeDecrease();
        setTheVolume();
    }   
    
    //decrease the volume.
    public void volumeDecrease(){
        if(Greenfoot.mouseClicked(this)){
            if(volume >= 5){
                volume -= 5;
                music.setVolume(volume);
            }
            System.out.println(volume);
        }
   }
     
    //Set the volume??(Help)
    public void setTheVolume(){
        MyWorld world = new MyWorld();
        world.setTheVolume(volume);
    }
}
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
public class volumeUp extends Actor
{
    private GreenfootSound music;
    private int volume;
     
      
    public volumeUp(){}
     
    public volumeUp(GreenfootSound music, int vol){
        this.music = music;
        this.volume = vol;
    }
     
    /**
     * Act - do whatever the volumeDown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        volumeIncrease();
         
        setTheVolume();
    }   
    
    //decrease the volume.
    public void volumeIncrease(){
       if(Greenfoot.mouseClicked(this)){
            if(volume <= 95){
                volume += 5;
                music.setVolume(volume);
            }
            System.out.println(volume);
        }
    }
     
    //Set the volume??(Help)
    public void setTheVolume(){
        MyWorld world = new MyWorld();
        world.getVolume();
        world.setTheVolume(volume);
    }
}
danpost danpost

2018/4/8

#
The VolumeUp object does not know when you decrease the volume in the VolumeDown object. That is to say, you have multiple volume fields -- one in each VolumeUp object, one in each VolumeDown object and also one in your MyWorld object. I would keep the one in your MyWorld object and control the volume from there. For that, you will need to keep references to the VolumeUp and VolumeDown object in your MyWorld object. Your MyWorld class would then be:
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
public class MyWorld extends World
{
    GreenfootSound music = new GreenfootSound("freebird.mp3");
    private GreenfootImage volumeB = new GreenfootImage(50, 60);
    private int volume = 100;
    Actor volumeUp, volumeDown;
     
    public MyWorld()
    {
        super(600, 400, 1);
        setButtons();
    }
     
    public void setButtons()
    {
        startButton start = new startButton(music);
        addObject(start, 300, 200);
         
        volumeDown = new VolumeDown();
        addObject(volumeDown, 70, 300);
         
        volumeUp = new VolumeUp();
        addObject(volumeUp, 70, 250);
    }
     
    public void act()
    {
        if (Greenfoot.mouseClicked(volumeUp) && volume < 100) adjustVolume(5);
        if (Greenfoot.mouseClicked(volumeDown) && volume > 0) adjustVolume(-5);
    }
     
    public void adjustVolume(int adjustment)
    {
        volume += adjustment;
        music.setVolume(volume);
    }
}
With the following replacement classes:
1
public class VolumeUp extends greenfoot.Actor {}
and
1
public class VolumeDown extends greenfoot.Actor {}
Do not know what line 4 is for. ???
t_o98 t_o98

2018/4/8

#
Thank you that worked! Line 4 is to implement a volume bar that I haven't really started on yet.
danpost danpost

2018/4/8

#
t_o98 wrote...
Thank you that worked! Line 4 is to implement a volume bar that I haven't really started on yet.
Strange for the image to be almost square. Also, it would seem appropriate for it to be in the class of the bar actor class itself -- not in the world class.
You need to login to post a reply.