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

2018/4/20

having a bit of trouble with setvolume

ohwooow ohwooow

2018/4/20

#
Hello there! I'm trying to create a media player and I'm having some difficultly. I'm been playing around with it for hours now and I just need some other eyes other then mines. Any help is appreciated!
public class MyWorld extends World
{

    private GreenfootSound sound  =  new GreenfootSound("win.mp3");
    private volUp volumeUp = new volUp();
    private volDown volumeDown = new volDown();
    private int volumeLevel = 50;
    
    

    
    /**
     * 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);
        addObject(new Start(),300, 200);
        addObject(new volUp(),500, 300);
        addObject(new volDown(),500, 350);
        
    }
    
    public volUp getVolUp()
    {
        return volumeUp ;
    }
    
    public volDown getVolDown()
    {
        return volumeDown;
    }
    
    public void setVolumeLevel(int volumeLevel)
    {
        this.volumeLevel = volumeLevel;
    }
    
    public GreenfootSound getSound()
    {
        return sound;
    }

    
}
public class volDown extends Start
{
    GreenfootSound sound;
    int volume = 100;
    

    
    /**
     * Act - do whatever the volDown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(((MyWorld)getWorld()).getVolDown())){
            setImage("volDown.png");
            MyWorld myWorld = (MyWorld)this.getWorld();
            GreenfootSound mySound = myWorld.getSound();
            volume -=10;
            if(volume<0)
            {
                volume=0;
            }
            mySound.setVolume(volume);            

        }
       
    }    
}
danpost danpost

2018/4/20

#
I see at least 3 problems: (1) the objects you add into the world are not the objects the getter methods in your MyWorld class return (getVolDown and getVolup); (2) you have two fields for the volume (at least) -- volume in volDown and volumeLevel in MyWorld; (3) you have the volDown class extending the Start class (they should both extend Actor, as well as the volUp class).
ohwooow ohwooow

2018/4/20

#
Alright Thanks! I've done the stuff you told me to do I think. Still not working
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 GreenfootSound sound  =  new GreenfootSound("win.mp3");
    private volUp volUp = new volUp();
    private volDown volDown = new volDown();
    private int volumeLevel = 50;
    
    

    
    /**
     * 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);
        addObject(new Start(),300, 200);
        addObject(new volUp(),500, 300);
        addObject(new volDown(),500, 350);
        
    }
    
    public volUp getVolUp()
    {
        return volUp ;
    }
    
    public volDown getVolDown()
    {
        return volDown;
    }
    
    public void setVolumeLevel(int volumeLevel)
    {
        this.volumeLevel = volumeLevel;
    }
    
    public GreenfootSound getSound()
    {
        return sound;
    }

    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class volDown here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class volDown extends Actor
{
    GreenfootSound sound;
    private int volumeLevel = 50;
    

    
    /**
     * Act - do whatever the volDown wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(((MyWorld)getWorld()).setVolumeLevel())){
            setImage("volDown.png");
            MyWorld myWorld = (MyWorld)this.getWorld();
            GreenfootSound mySound = myWorld.getSound();
            myWorld.setVolumeLevel(volumeLevel) -=10;
            if(volumeLevel<0)
            {
                volumeLevel=0;
            }
            mySound.setVolumeLevel(volumeLevel);            

        }
       
    }    
}
danpost danpost

2018/4/20

#
You still have issues (1) and (2). You only fixed (3).
You need to login to post a reply.