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