I'm currently working on the textbook exercises 10.45 - 10.50 in Ch 10 of the 2nd edition textbook. It wants me to create a kind of mp3 player thingy. Everything is peachy until I got to the portion where it wants me to change the volume in 10.48. I'm at a complete loss here, am I using the GreenfootSound class incorrectly? I have a reference to the GreenfootSound object in my volumeDown button however, when I attempt to execute the setVolume() method of my GreenfootSound instance called music it only reduces the volume by 4 when its supposed to be 10, then it dosent do anything. I'm feeling pretty dumb here...
Source...
Code for MyWorld...
and code for the button...
Im at a loss here, when I send getVolume to output it gives me some funky stuff...
any help would be greatly appreciated, Thanks.
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { private VolumeBar volumeBar; private GreenfootSound music; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); prepare(); } private void prepare() { music = new GreenfootSound( "Every Planet We Reach Is Dead.mp3" ); PlayButton play = new PlayButton(music); addObject(play, 55 , 350 ); volumeBar = new VolumeBar(); addObject(volumeBar, 545 , 210 ); VolumeUpButton volumeUp = new VolumeUpButton(volumeBar, music); addObject(volumeUp, 450 , 350 ); VolumeDownButton volumeDown = new VolumeDownButton(volumeBar, music); addObject(volumeDown, 350 , 350 ); VolumeBarContainer volumeBarContainer = new VolumeBarContainer(); addObject(volumeBarContainer, 545 , 210 ); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class VolumeUp here. * * @author (your name) * @version (a version number or a date) */ public class VolumeUpButton extends Button { private VolumeBar volumeBar; private GreenfootSound music; /** * Act - do whatever the VolumeUp wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public VolumeUpButton(VolumeBar volumeBar, GreenfootSound music) { this .volumeBar = volumeBar; this .music = music; } public void act() { //For testing purposes... //System.out.print(music.getVolume() + "\n"); onClick(); } public void onClick() { if (Greenfoot.mouseClicked( this ) && volumeBar.getImage().getHeight() < 360 ) { //For testing purposes... //System.out.print(music.getVolume() + "\n"); volumeBar.getImage().scale(volumeBar.getImage().getWidth(),volumeBar.getImage().getHeight() + 36 ); volumeBar.setLocation(volumeBar.getX(), volumeBar.getY() - 18 ); music.setVolume(music.getVolume()+ 10 ); } } } |