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

2012/4/9

Sounds for Movement

lgbrf lgbrf

2012/4/9

#
I want my player to make a sound when it moves, and i have programmed this in. The problem is it loops the sound on top of itself, how can i make it play once and not play again until it has finished? this is the code for the two methods i used: private void moveControllable() { if(Greenfoot.isKeyDown("left")) { move(-4); play(); } if(Greenfoot.isKeyDown("right")) { move(4); play(); } if(Greenfoot.isKeyDown("up")) { setLocation( getX(), getY()-4); play(); } if(Greenfoot.isKeyDown("down")) { setLocation( getX(), getY()+4); play(); } } private void play() { Greenfoot.playSound("pacman_chomp.wav"); };
tkiesel tkiesel

2012/4/9

#
Make your sound into a GreenfootSound object. At the top of the class, with other variables:
GreenfootSound chomp = new GreenfootSound("pacman_chomp.wav");
Then, in play()
private void play() 
{ 
    chomp.play(); 
}
lgbrf lgbrf

2012/4/9

#
That worked great, thanks :D
You need to login to post a reply.