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

2015/12/15

Play audio when mouse hovers over an object.

1
2
arw90 arw90

2015/12/15

#
I want an audio file to play when the user hovers over an actor. Does anyone know a good way to do this?
danpost danpost

2015/12/15

#
Be more specific. It sounds like you want the audio file to play in a loop as long as the mouse is hovering and pause when the mouse moves off the object -- but one cannot be sure.
arw90 arw90

2015/12/15

#
I want the audio file to play only once when someone hover overs it.
danpost danpost

2015/12/15

#
Then you just need a boolean field to track the hover state of the mouse on the object. When the state changes, change the value of the field and if the field after being changed is true, then play the sound.
arw90 arw90

2015/12/15

#
I have no idea how to pull that off.
arw90 arw90

2015/12/15

#
1
2
3
4
5
6
7
public void audio()
{
    if (Greenfoot.mouseMoved(this))
    {
        Greenfoot.playSound("HardVoice.mp3");
    }
}
This is what I have but it does not play the audio once, it plays it over and over.
danpost danpost

2015/12/15

#
arw90 wrote...
< Code Omitted > This is what I have but it does not play the audio once, it plays it over and over.
That is why you need to track the hover state:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// instance field
private boolean hovering;
 
// act code of mouse hover object
if (Greenfoot.mouseMoved(null)) // mouse moved?
{
    if (hovering != Greenfoot.mouseMoved(this)) // change in hover state?
    {
        hovering = ! hovering; // save change
        if (hovering) // hover begins?
        {
            // play sound
        }
    }
}
arw90 arw90

2015/12/15

#
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class Hard here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Hard extends Difficulty
{
 
    public Hard()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() + 100, image.getHeight() + 100);
        setImage(image);
    }
 
    private boolean hovering;
 
    public void audio()
    {
        if (Greenfoot.mouseMoved(this))
        {
            Greenfoot.playSound("HardVoice.mp3");
        }
    }
    if (Greenfoot.mouseMoved(null))
    {
        if (hovering != Greenfoot.mouseMoved(this))
        {
            hovering = ! hovering;
            if (hovering)
            {
                Greenfoot.playSound("HardVoice.mp3")
            }
        }
    }
    /**
     * Act - do whatever the Hard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        startHard();
    }   
 
    public void startHard()
    {
        if (Greenfoot.mouseClicked(this))
        {
            hardGame();
            getWorld().removeObjects(getWorld().getObjects(Easy.class));
            getWorld().removeObjects(getWorld().getObjects(EasyText.class));
            getWorld().removeObjects(getWorld().getObjects(Medium.class));
            getWorld().removeObjects(getWorld().getObjects(MediumText.class));
            getWorld().removeObjects(getWorld().getObjects(HardText.class));
            getWorld().removeObjects(getWorld().getObjects(Credits.class));
            getWorld().removeObjects(getWorld().getObjects(Hard.class));
        }
    }
 
    public void hardGame()
    {
        getWorld().addObject(new Wolf(), 200, 275);
        getWorld().addObject(new Octopus(), 500, 275);
        getWorld().addObject(new Chicken(), 800, 275);
    }
 
}
This is my entire code and when I try to compile I get an error (illegal start of type) and I do not know why.
danpost danpost

2015/12/15

#
arw90 wrote...
< Code Omitted > This is my entire code and when I try to compile I get an error (illegal start of type) and I do not know why.
You did not place the code I provided within any methods (hence the error). You can replace the code in the 'audio' method with it; but, make sure you call it from the 'act' method.
arw90 arw90

2015/12/15

#
Just to make sure should I have it as a public or private void:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void playHint()
{
    private boolean hovering()
    {
        if (Greenfoot.mouseMoved(null))
        {
            if (hovering != Greenfoot.mouseMoved(this))
            {
                hovering = ! hovering;
                if (hovering)
                {
                    Greenfoot.playsound("PlaceHolder.mp3");
                }
            }
        }
    }
}
danpost danpost

2015/12/15

#
arw90 wrote...
Just to make sure should I have it as a public or private void
Usually, if the method is only to be called from within the class, it should be declared as 'private'. There is also a 'protected' access modifier which allows subclasses to access the method, but not other classes. Refer to this java tutorial page for more.
arw90 arw90

2015/12/15

#
Thank you so much for your help, I have learned so much!
danpost danpost

2015/12/15

#
It seems like quite a bit of code to remove the control objects one at a time. You should be able to remove them all at once:
1
2
3
4
5
World world = getWorld();
world.removeObjects(world.getObjects(null));
world.addObject(new Wolf(), 200, 275);
world.addObject(new Octopus(), 500, 275);
world.addObject(new Chicken() 800, 275);
Replacing lines 52 through 67 with this should do the same thing (unless there are other objects in the world I am not aware of).
arw90 arw90

2015/12/15

#
Oh, thanks! By the way greenfoot cannot find symbol - variabl hovering, do you know why?
danpost danpost

2015/12/15

#
arw90 wrote...
Oh, thanks! By the way greenfoot cannot find symbol - variabl hovering, do you know why?
Yeah. Line 3, in your last code post, should be outside the method and should not have parenthesis after the variable name.
There are more replies on the next page.
1
2