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

2018/6/10

Greenfoot.delay(); not pausing my sounds

1
2
ogy2014 ogy2014

2018/6/10

#
when i shoot a space invader and when i get shot it has a slight delay so you can actually see the damagedimage and also i think it gives it gives a retro vibe, one problem is that when my ufo goes atop of the screen it has a 12 second sound clip playing but the delay doesnt pause it so if i hit a bunch of space invaders within the 12 seconds the sound clip will finish way before the ufo gets to the other end of the screen.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    /**
     * Constructor for objects of class Space.
     * 
     */
    GreenfootSound ufo = new GreenfootSound("ufo.wav");
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        addObject (gameover, 400, 300);
        addObject (lifeboard, 500, 20);
        addObject (scoreboard, 100, 20);        
        addObject (new Player(), 400, 550);
        addObject (new Invaders(), 0, 0);
        addObject (new Barrier1(), 200, 475);
        addObject (new Barrier2(), 400, 475);
        addObject (new Barrier3(), 600, 475);
        setPaintOrder(GameOver.class,Player.class);
    }
    
    public void act()
    {
        if(getObjects(UFO.class).size() == 0)
        {
            ufo.stop();
            if(Greenfoot.getRandomNumber(3000) == 1500)
            {
                addObject(new UFO(), 0, 50);
                ufo.play();
                return;
            }
        }
    }
    
    public Scoreboard scoreboard = new Scoreboard();                               
    public Lifeboard lifeboard = new Lifeboard();
    public GameOver gameover = new GameOver();
}
i am using Greenfoot.delay(); for the delay
ogy2014 ogy2014

2018/6/10

#
i think i made it more confusing than it is. Is there a delay that pauses everything including sounds? or is there a way to pause that sound in particular because the other sounds are only 0.5 of a second long?
danpost danpost

2018/6/10

#
ogy2014 wrote...
is there a way to pause that sound
The sound, ufo, is a GreenfootSound object. Look in the GreenfootSound class API documentation to see what other methods, besides the play method, are available.
ogy2014 ogy2014

2018/6/10

#
i cant figure out how to use the soundfile.pause(); code
Actor invadermissile;
        invadermissile = getOneIntersectingObject(InvaderMissile.class);
        if (getY() == getWorld().getHeight() -1)
        {
            getWorld().removeObject(this);
            return;
        }
        if(invadermissile != null)
        {
            Space space = (Space) getWorld();
            Lifeboard lBoard = space.lifeboard; 
            lBoard.add(-1);
            getWorld().removeObject(invadermissile);
            this.setImage(explosion);
            Greenfoot.playSound("explosion.wav");
            Greenfoot.delay(100);
            this.setImage(player);
            return;
        }
        
        Actor invader = getOneIntersectingObject(Invader.class);
        if(invader != null)
        {
            Space space = (Space) getWorld();
            GameOver gBoard = space.gameover;        
            this.setImage(explosion);
            Greenfoot.playSound("explosion.wav");
            Greenfoot.delay(50);
            space.gameover.add(1);
            return;
        }    
ogy2014 ogy2014

2018/6/10

#
this in in my space actor
    public void act()
    {
        if(getObjects(UFO.class).size() == 0)
        {
            ufo.stop();
            if(Greenfoot.getRandomNumber(3000) == 1500)
            {
                addObject(new UFO(), 0, 50);
                ufo.play();
                return;
            }
        }
    }
ogy2014 ogy2014

2018/6/10

#
the .play(); is in my space code for when the ufo spawns in randomly and the delays are in my player actor's code
ogy2014 ogy2014

2018/6/10

#
i thought about making 1 or 2 new classes but i dont know if that would work (2 different classes for the different delay times)
danpost danpost

2018/6/11

#
ogy2014 wrote...
i thought about making 1 or 2 new classes but i dont know if that would work (2 different classes for the different delay times)
Preferably, you should not use the delay method of the Greenfoot class at all in the middle of a game. I wonder why you would want to have everything hesitate every time an explosion occurs. If it is for the explosion sound, then take note that two sounds can be made to happen coincidentally (at the same time). You can have a continuous sound (like for the UFO) along with an explosion sound which can occur at any time the UFO sound is playing.
ogy2014 ogy2014

2018/6/11

#
I understand that most of the time it is bad to use but I am happy with how the game has turned out and I am just wondering if there was a bit of code I could put in to stop the sound with the rest of the game.
ogy2014 ogy2014

2018/6/11

#
But if I must do it I am just wondering if the timer method (I presume) of pausing the game is complicated to implement
ogy2014 ogy2014

2018/6/11

#
I think I might know how to do this now, I didn't realize that .pause(); can be used to start playing once paused, so if I make 2 new score counter classes that get 1 added to before the pause happens if the UFO sound is playing, then they (I know it's kinda lazy coding) just delay the game at the same time as what happens in the player actor but before and after it does the delay it pauses the UFO sound, if that makes any sense
danpost danpost

2018/6/11

#
ogy2014 wrote...
I think I might know how to do this now, I didn't realize that .pause(); can be used to start playing once paused, so if I make 2 new score counter classes that get 1 added to before the pause happens if the UFO sound is playing, then they (I know it's kinda lazy coding) just delay the game at the same time as what happens in the player actor but before and after it does the delay it pauses the UFO sound, if that makes any sense
Score counter classes? really!! Change the act method in your Space class to this:
public void act()
{
    if (getObjects(UFO.class).isEmpty())
    {
        if (ufo.isPlaying()) ufo.stop();
        if (Greenfoot.getRandomNumber(3000) == 1500) addObject(new UFO(), 0, 50);
    }
    else if (!ufo.isPlaying()) ufo.playLoop();
}
Then make the GreenfootSound ufo object public:
public GreenfootSound ufo = new GreenfootSound("ufo.wav");
Now, when exploding (in the other class):
((Space)getWorld()).ufo.pause();
GreenfootSound explosion = new GreenfootSound("explosion.wav");
explosion.play();
while (explosion.isPlaying()) {}
The last line will "pause" (or delay) everything until the explosion sound finishes. The act method of the Space class will resume the ufo sound, if needed.
ogy2014 ogy2014

2018/6/11

#
now when the ufo explodes the sound continues for around a second more than it should
ogy2014 ogy2014

2018/6/11

#
one sec ill modify the looping sound i think that is the problem
ogy2014 ogy2014

2018/6/11

#
it didnt work
There are more replies on the next page.
1
2