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

2017/12/27

Pew Sound Problems

SuperNinja SuperNinja

2017/12/27

#
I've encountered an issue for when I try to call a sound a play when I shoot a dart. My actor has an if statement to see if the space key is down, and if a counter is above a number. If both are true, the dart spawns and the sound plays, but the problem is that the sound only plays for the first dart and then randomly after that. I've had a similar sound function for when the actor gets hit, and the sound plays even if the there's another hit immediately after so I don't think it has anything to do with the length of the sound.
1
2
3
4
5
6
7
8
9
10
11
public void pewSound()
    {
        pew.play();
    }
 
if (Greenfoot.isKeyDown("space") && DartLimiter > 8 && power==0){
            DartLimiter = 0;//dart spam reset
            pewSound();
            DartThrow fire = new DartThrow();//the actual shooting
            getWorld().addObject(fire, getX() + 3, getY() + 1);//adds the dart
        }
Thanks in advance
xbLank xbLank

2017/12/27

#
It has something to do with your conditions. Remove DartLimiter > 8 and power == 0 out of your if statement and check if the sound plays properly. If that's the case then you are messing up the assigning part.
xbLank xbLank

2017/12/27

#
I am currently using this Method for my SpaceWar game:
1
2
3
4
5
6
7
8
private void Shoot()
    {
        if(currentAmmo > 0)
            currentAmmo--;
        getWorld().addObject(new Bullet(getRotation()), getX()+rocketWidth, getY());
        Greenfoot.playSound("/SFX/shoot.wav");
        wait += (currentAmmo > 0) ? reloadtime : reloadtime*multiplier;
    }
danpost danpost

2017/12/27

#
The length of the sound may have something to do with it -- particularly if you set the following:
1
GreenfootSound pew = new GreenfootSound("pew.wav");
This line creates one, and only one, instance of the sound. If you try "pew.play()" while 'pew' is already playing, it does not restart or play again. Use:
1
Greenfoot.playSound("pew.wav");
to play the sound to allow multiple "pew" sounds to play simultaneously. This construct creates a new distinct GreenfootSound object each time it is executed.
SuperNinja SuperNinja

2018/1/3

#
Wait, can you still change volume if you do it this way?
danpost danpost

2018/1/3

#
SuperNinja wrote...
Wait, can you still change volume if you do it this way?
Not when using 'playSound'. For multiple sounds where volume is controlled, do not use a field as in the first way above. Instead, create the GreenfootSound object in the code itself when firing the dart:
1
2
3
GreenfootSound pew = new GreenfootSound("pew");
pew.setVolume(50); // or whatever
pew.play();
You need to login to post a reply.