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

2017/11/19

Adding sound when object is collected

MattTheGooner MattTheGooner

2017/11/19

#
So I'm having a little problem, the sound file does not play and the program stops running. Here is the code ; public void collect() { if(isTouching(Jhope.class)) { removeTouching(Jhope.class); Greenfoot.playSound("Jhope_chant.wav"); }
danpost danpost

2017/11/19

#
If the program unexpectedly stops, then you should be getting some error message in a terminal window. What message are you getting? (best is to copy/paste entire output here)
MattTheGooner MattTheGooner

2017/11/19

#
I made a simple mistake and all but one sound is playing. The final object to be collected plays but it doesn't sound right. Can it be that all the sounds are playing at the same time when the game ends? Here's the code; public void collect() { if(isTouching(Jhope.class)) { removeTouching(Jhope.class); Greenfoot.playSound("Jhope_chant.wav"); } if(isTouching(Jungkook.class)) { removeTouching(Jungkook.class); Greenfoot.playSound("Jungkook_chant.wav"); } if(isTouching(Jimin.class)) { removeTouching(Jimin.class); Greenfoot.playSound("Jimin_chant.wav"); } if(isTouching(Suga.class)) { removeTouching(Suga.class); Greenfoot.playSound("Suga_chant.wav"); } if(isTouching(Jin.class)) { removeTouching(Jin.class); Greenfoot.playSound("Jin_chant.wav"); } if(isTouching(Rm.class)) { removeTouching(Rm.class); Greenfoot.playSound("RM_chant.wav"); } if(isTouching(V.class)) { removeTouching(V.class); Greenfoot.playSound("V_chant.wav"); } if(isTouching(Bts.class)) { Greenfoot.playSound("Bts_chant.wav"); World MyWorld = getWorld(); GameOver gameover = new GameOver(); MyWorld.addObject(gameover, MyWorld.getHeight()/2, MyWorld.getWidth()/2); } }
danpost danpost

2017/11/19

#
You should note that all other sounds are triggered by objects that are immediately being removed from the world. With the last one, the Bts object is not being removed -- nor is a different world being set active. This means that the act method of this object is repeatedly finding itself intersecting the Bts object and repeatedly creating new GreenfootSound objects to play, one on top of another. Hence, the impression of all your sounds playing at the same time. One way to "fix" the issue would be to remove the intersecting Bts object from the world. However, if you are to keep it in the world, you will need to add some other condition along with 'isTouching' to control the block of code. Since you cannot tell whether the sound file is already playing or not (and you probably do not want to have that sound loop either), the only other thing that is different is that you added a GameOver object into the world. So, try this:
if (isTouching(Bts.class) && getWorld().getObjects(GameOver.class).isEmpty())
You need to login to post a reply.