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

2012/12/13

stopping music when world ends

1
2
bbwf bbwf

2012/12/13

#
So in my game when the world starts it plays music, but when the world ends it doesn't stop playing music, help please!
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
{
//private Score score;

    /**
     * Constructor for objects of Space
     * 
     */


    public Space()
    {    
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.
        super(850, 550, 1,false);
        addObject(new Aligator(),0,250);
        addObject(new Camel(),625,283);
        //addObject(new Score(),50,50);
        int count =0;
                
          {  
                Greenfoot.playSound("Sound.wav");
            }
            
        while(count<=30)
            {
                addObject(new Frog(),Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(500));
               count = count+1;
               
            }
        }
        
     
   // public void addPoints()
    //{
        //score.addPoints();
   // }
   // public void addLittlePoints()
   // {
     //  score.addLittlePoints();
   // }
   // public void subtractPoints()
   //{
      //  score.subtractPoints();
   // }
}

Gevater_Tod4711 Gevater_Tod4711

2012/12/13

#
if you create an object from type GreenfootSound you can start and stop it's playing.
bbwf bbwf

2012/12/13

#
huh?
davemib123 davemib123

2012/12/13

#
first thing you do is define the sound as a variable:
private GreenfootSound bgSound;
then define location:
bgSound = new GreenfootSound("background.mp3");
I created two methods the first to have sound start:
    public void started()
    {
        bgSound.playLoop();
    }
the second to stop the sound when the game is over:
  public void gameOver()
    {
        bgSound.stop();
     }
hope that helps :)
bbwf bbwf

2012/12/14

#
Where would I put this?
davemib123 davemib123

2012/12/14

#
in the world class
bbwf bbwf

2012/12/14

#
After act? Or in act?
davemib123 davemib123

2012/12/14

#
I dont have act in my World class. post up your world class
bbwf bbwf

2012/12/14

#
And if I do this it says "Identifier expected.
bbwf bbwf

2012/12/14

#
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
{
//private Score score;

    /**
     * Constructor for objects of Space
     * 
     */


    public Space()
    {    
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.
        super(850, 550, 1,false);
        addObject(new Aligator(),0,250);
        addObject(new Camel(),625,283);
        //addObject(new Score(),50,50);
        int count =0;
                
       //Greenfoot.playSound("Light.mp3");
            
        while(count<=30)
            {
                addObject(new Frog(),Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(500));
               count = count+1;
               
            }
        }
        
     private GreenfootSound bgSound;
     bgSound = new GreenfootSound("Light.mp3");
         public void started()
    {
        bgSound.playLoop();
    }
      public void gameOver()
    {
        bgSound.stop();
     }

   // public void addPoints()
    //{
        //score.addPoints();
   // }
   // public void addLittlePoints()
   // {
     //  score.addLittlePoints();
   // }
   // public void subtractPoints()
   //{
      //  score.subtractPoints();
   // }
}
davemib123 davemib123

2012/12/14

#
woops I read my file wrong. I do hve an Act method. The act method I have to check if the health bar goes to 0. If it does then read the gameover function. but the start of your world class should be:
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
{
    private GreenfootSound bgSound;

    public Space()
    {    
        super(850, 550, 1,false);
        addObject(new Aligator(),0,250);
        addObject(new Camel(),625,283);
        bgSound = new GreenfootSound("Light.mp3");
        int count =0;
        while(count<=30)
        {
            addObject(new Frog(),Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(500));
            count = count+1;
        }
    }

    public void act(){ //need some function to check for gameover
        if (bar.value == 0){
            gameOver();
        }
    }

    public void started()
    {
        bgSound.playLoop();
    }

    public void gameOver()
    {
        bgSound.stop();
    }

}
bbwf bbwf

2012/12/14

#
I do not have a health bar though? The only way the world ends is it the camel touches the Alligator.
davemib123 davemib123

2012/12/14

#
so the function which checks for collision between camel and alligator should be in the Act method
bbwf bbwf

2012/12/17

#
I'm sorry but I'm confused.
NoobCoder NoobCoder

2015/3/22

#
I need to understand this so bad too am trying to get the sound to stop when my character dies by colliding into something
There are more replies on the next page.
1
2