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


1 2 3 4 5 6 7 | public void audio() { if (Greenfoot.mouseMoved( this )) { Greenfoot.playSound( "HardVoice.mp3" ); } } |
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 } } } |
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 ); } } |
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" ); } } } } } |
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 ); |