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

2020/10/31

Help me to fix SOUND!!

astawakadek astawakadek

2020/10/31

#
*/ i want to stop my background music, but i can't, when the hero is die or alive Score are 0, the music is stop, some body to help me? */
import greenfoot.*;  

public class MyWorld extends World
{
    
     
    public static GreenfootSound music = new GreenfootSound("bg.wav");
     
     
     public void bgMusic()
     {  
         if (MyWorld.aliveScore.getValue()==0)
         { 
             music.stop();
            }
         else
         {
             music.playLoop();
            }
        }
        

    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        
        
        
      
    }
    
    public void act(){
      
      this.createEnemy();
      this.createLaserEnemy();
      this.bgMusic();

    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    
    public static Counter score = new Counter("Score : ");
    public static Alive aliveScore = new Alive ("Alive : ");
    private void prepare()
    {
        Hero hero = new Hero();
        addObject(hero,70,200);

        addObject(score,50,20);
        score.setValue(0);

        addObject(aliveScore,150,20);
        aliveScore.setValue(5);

    }
    
      
    int enemyCounter=0;
    private void createEnemy(){
        enemyCounter++;
        
        if (enemyCounter==60){
            Enemy enemy = new Enemy(Greenfoot.getRandomNumber(7)+1);
            addObject(enemy, 600, Greenfoot.getRandomNumber(400));
            enemyCounter = 0;
        }
    }
    
   int LaserEnemyCounter=0;
    private void createLaserEnemy(){
        LaserEnemyCounter++;
        
        if (LaserEnemyCounter==60){
            LaserEnemy enemy = new LaserEnemy(Greenfoot.getRandomNumber(10)+1);
            addObject(enemy, 600, Greenfoot.getRandomNumber(400));
            LaserEnemyCounter = 0;
        }
    }
    
    
    
}
danpost danpost

2020/10/31

#
Instead of the bgMusic method in your MyWorld class, you should check the aliveScore value at the time it decreases (probably in your Hero class) and stop the music there.
astawakadek astawakadek

2020/10/31

#
Thank you, I didn't think about it, and it's going well!
You need to login to post a reply.