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

2020/12/12

How to use setVolume() to control volume?

akiki akiki

2020/12/12

#
I am trying to use the "setVouume()" to control the volume but it not work. I cannot find out the problem. Could anyone teach me? Thank you.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start extends Actor
{
    private GreenfootSound mySound = new GreenfootSound("Twice - I CAN'T STOP ME.mp3");
    private int volumeA = 50;
    
    public Start()
    {

    }

    public Start(GreenfootSound mySound, int volumeA)
    {
        this.mySound = mySound;
        this.volumeA = volumeA;
    }

    public GreenfootSound getMySound()
    {
        return mySound;
    }

    public int getVolumeA()
    {
        return volumeA;
    }

    public void setMySound(GreenfootSound mySound)
    {
        this.mySound = mySound;
    }

    public void setVolumeA(int volumeA)
    {
        this.volumeA = volumeA;
    }

    /**
     * Act - do whatever the Start wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        startButton();
        curVolume();
    }

    public void startButton()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(!(mySound.isPlaying()))
            {
                mySound.play();
                setImage("stop.png");
            }
            else
            {
                mySound.pause();
                setImage("start.png");
            }
        }
    }

    public void curVolume()
    {
        mySound.setVolume(volumeA);
    }

    public void volUp()
    {

        if(volumeA < 100)
        {
            volumeA = volumeA + 5;
            curVolume();
        }

    }
    public void volDown()
    {
        if(volumeA > 0)
        {
            volumeA = volumeA - 5;
            mySound.setVolume(volumeA);
        }
    }
}
This is VolumeUp class

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 VolumeUp extends Start
{
    
    
    public VolumeUp()
    {
        
    }
    
    
    /**
     * Act - do whatever the VolumeUp wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        up();
        
    }    
    
    public void up()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Start start = new Start();
            start.volUp();
            
        }
    }
}
danpost danpost

2020/12/12

#
Apparently, you do not understand what it means when you make a class extend another (subclassing). A subclass is to describe the same type object as its parent class. Since a VolumeUp object is NOT a Start object, neither should extend the other. A subclass usually describes the object more precisely. The Object class is the root of ALL classes. Classes describe objects. The Actor class, which extends Object, describes any generic actor. All its subclasses will describe some actor type more specifically. Actor might be subclassed by Button, for a generic button actor object (notice the last three words). Button, in turn, could by extended by Start, VolumeUp and VolumnDown -- describing 3 distinct actors. Here, the end of the road is reached. Do not take this as meaning your classes should be as described here. Just take from it what extending a class should be doing. Lines 33 and 34 in your VolumeUp class clearly shows another misconception. Here, you create a NEW Start object, which, in turn, creates a NEW GreenfootSound object. Changing the volume of this new sound object does nothing for the sound that might currently be playing. Without some general background and some details as to how this relates to your project, it would be impossible to nail down how you should proceed.
You need to login to post a reply.