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

2016/11/10

Sound starts playing despite using !isPlaying

Saberdeer Saberdeer

2016/11/10

#
I recently started dabbling in Greenfoot, and so far it's working fine. However, I want to play a sound whenever S is pressed down. Naturally, I don't want it to start every single frame anew. I tried looking it up via Google, and while it seems to help everybody, it somehow doesn't work for me:
GreenfootSound lasersound = new GreenfootSound("shot_laser.wav");
if (!lasersound.isPlaying())
{
     lasersound.play();
}
This is how it's currently set up, but it's still playing every single frame (which of course makes it horribly annoying), and I don't know how to fix it. The code above is from my PlayerShip class (Actor > Spaceship > PlayerShip). I'm sure I'm missing something stupidly obvious, so any input would be appreciated.
danpost danpost

2016/11/10

#
You probably have line 1 in the given code above inside the method, where it becomes a new variable every frame step. Move the line outside the method so that it is not reinitialized every step.
Saberdeer Saberdeer

2016/11/10

#
Aaaand I feel dumb. Yeah, that fixed it. Thank you!
You need to login to post a reply.