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

2016/4/15

mouseClick function

GreenMan15 GreenMan15

2016/4/15

#
Hello, I am trying to add a play button and when I click on it it starts playing, and then when I click on it again it pauses and a pause button shows up. But I'm not sure what is wrong with my code. Thoughts?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(Greenfoot.mouseClicked(this))
        {
            mySound.play();
        }
        else
        {
            if(mySound.isPlaying())
            {
                if(Greenfoot.mouseClicked(this))
                {
                    mySound.pause();
                    setImage("Pause.png");
                }
            }
 
        }
danpost danpost

2016/4/15

#
The block between lines 6 and 16 is executed if the button is not clicked. So, the condition for the 'if' on line 9 will always be 'false' and lines 11 and 12 will never be executed. Try to improve the logic here (ask yourself what conditions are needed to start the music and then to stop the music). HINT: the 'else' on line 5 is at fault here (you do not want to do anything if the mouse was not clicked).
GreenMan15 GreenMan15

2016/4/15

#
Alright appreciate the feedback, at least I know why the else statement isn't neccessary
danpost danpost

2016/4/15

#
GreenMan15 wrote...
Alright appreciate the feedback, at least I know why the else statement isn't neccessary
You will probably still need an 'else' -- not for the 'if' of the mouse click, but for whether the sound is currently playing or not.
GreenMan15 GreenMan15

2016/4/15

#
Oh your absolutely right, it works now!
GreenMan15 GreenMan15

2016/4/16

#
I created two other classes, one is a button for the volume to go up, the volume to go down, I left the act methods empty on those classes and tried to code it in the Start button method. I really need advice, I've been doing this for 3 hours straight with no progress. Line 28-41 is where I'm stuck at, when I uncomment the line mySound.setVolume(volume), the volume is set but i'm trying to decrease the volume when I click on the LowerVolume button when I run it
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
44
45
public class Start extends Actor
{
    GreenfootSound mySound = new GreenfootSound("Guitar.mp3");
    public int volume = 50;
    public Start()
    {
        GreenfootImage myImage = getImage();
        int newHeight = myImage.getHeight()/2;
        int newWidth = myImage.getWidth()/2;
        myImage.scale(newHeight, newWidth);
    }
 
    /**
     * 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()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(mySound.isPlaying())
            {
                mySound.stop();
                setImage("PlayButton.png");
            }
            else
            {
             //mySound.setVolume(volume);
             setTheVolume();
             mySound.playLoop();
             setImage("Pause.png");
            }
        }
         
    }
    public void setTheVolume()
    {
        if(Greenfoot.mouseClicked(LowerVolume.class))
        {
            volume--;
            mySound.setVolume(volume);
        }
         
    }
}
danpost danpost

2016/4/16

#
There are several issues with the code as given: (1) line 38 has a class as the type for the parameter of a method that requires an Actor type object; (2) line 29 call the 'setTheValue' method only when the Start button is clicked; so, line 38 will always be false (if you put a LowerValue object as the parameter value); you cannot click on two different type of objects at the same time; (3) as the code is written, the volume will decrease one unit every time the sound is unpaused (when the PlayButten is clicked. (4) in a general sense, you should not be using the same user action for more than one purpose unless you want both to occur simultaneously; I would suggest you use one user action (the mouse click on the button) to control the playing of the sound (on and off) and another action for setting the volume. You could use a slider or a bar whose value can be changed by the mouse or you could use keyboard input to control the volume; you could even use two separate buttons -- one for increasing and one for decreasing the volume.
GreenMan15 GreenMan15

2016/4/16

#
OH I know what you mean! I will try that
GreenMan15 GreenMan15

2016/4/16

#
After hours of trying to figure this out, I finally understand what to do. I did what you said and use two buttons one for high volume and one for low volume and it works perfectly. Thanks a bunch and Cheers!
You need to login to post a reply.