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

2014/5/20

Sound Not Working

1
2
mandyg233 mandyg233

2014/5/20

#
^^^ I tried that it didn't work
danpost danpost

2014/5/20

#
I believe the Greenfoot method 'playSound' causes the sound to be looped automatically; and there is no control over that sound once it is started. Making a GreenfootSound object will allow the methods in its class to control the sound object. Just by using the method 'play', it will play just one time; there is no need to stop or pause it.
mandyg233 mandyg233

2014/5/20

#
Dan isn't that what I'm doing in the code above though?
mandyg233 mandyg233

2014/5/20

#
This is still making it loop: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Boat here. * * @author (your name) * @version (a version number or a date) */ public class Boat extends Actor { private static GreenfootSound soundSound = new GreenfootSound("crack.mp3"); /** * Act - do whatever the Boat wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (getX() >1185) { move(-5); } if (getX() == 1185) { getWorld().addObject(new Label(), 400, 249); soundSound.play(); } } }
danpost danpost

2014/5/20

#
Ok. I think what is happening is that your actor is still at x=1185; and as soon as the sound stops, the code re-starts it. You may need to add a boolean field 'soundPlayed' to the code and set it to 'true' when starting the sound:
1
2
3
4
5
6
7
8
9
// add instance field
private boolean soundPlayed;
// the 'if' block
if (getX() == 1185 && !soundPlayed)
{
    getWorld().addObject(new Label(), 400, 249);
    soundSound.play();
    soundPlayed = true;
}
You probably had a bunch of Label objects on top of each other also.
mandyg233 mandyg233

2014/5/20

#
Yay it works!! Thanks so much
wabuilderman wabuilderman

2014/5/20

#
sorry for me not thinking of that. glad its working tough.
mandyg233 mandyg233

2014/5/20

#
Hey no worries I have like no idea really what I'm doing
lordhershey lordhershey

2014/5/20

#
--
You need to login to post a reply.
1
2