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

2012/10/28

Music wont stop playing...

SimpleJack SimpleJack

2012/10/28

#
Hi, I have a welcomeScreen world that starts playing the game song and a gameOver world when the player dies. this is my Code.. welcomeScreen:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)



public class welcomeScreen extends World
{



boolean canPlay = true;
static GreenfootSound music = new GreenfootSound("music.mp3");


    /**
     * Constructor for objects of class welcomeScreen.
     * 
     */
    public welcomeScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 500, 1); 
        //addObject(new titleOverlay() , 477, 88);
        addObject(new buttonStart(), 450, 338);

        
    }
    
    public void act()
    {
        music = new GreenfootSound("music.mp3");
        
        if (canPlay)
        {
            music.play();
            canPlay = false;
        }
        
        
    }

}
gameOver:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)



public class gameOver extends World
{
     boolean played = false;
     int timer = 0;
     int H, W;
     GreenfootSound gameOver = new GreenfootSound("gameover.mp3");
     welcomeScreen welcomeScreen = new welcomeScreen();
     
    public gameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 500, 1); 
        
        addObject(new gameOverText(), 459,82);
        gameOver.play();
    }
    
    public void act()
    {
        timer++;
        H = getBackground().getHeight();
        W = getBackground().getWidth();
        scaleImage();
        
        if (welcomeScreen.music.isPlaying() )
        {
            welcomeScreen.music.stop();
        }
        
        if (!gameOver.isPlaying() )
        {
            Greenfoot.stop();
        }
        
        
    }
  
    
    public void scaleImage()
    {
        
    }
}
danpost danpost

2012/10/28

#
Remove line 11 from your gameOver world. You are creating a new world instance in which the music has not started in, the command to stop the music does essentially nothing. Also, you do not need to ask if the music is playing, just command to stop the music and if is it playing it will stop; and if it is not playing, nothing will happen and no error will ensue. So, remove lines 29, 30 and 32; and move line 31 to the constructor, before the 'gameOver.play();' statement. Another hold-back on getting your music to stop playing: in the welcomeScreen world you are assigning the GreenfootSound object 'music' in the declaration of the variable by creating a new GreenfootSound object there. Then in the act method you are creating another GreenfootSound object and commanding that one to play (if not playing). The one saved in your variable is never started, and the one that continues to play you cannot stop because you do not have a reference to it. Remove line 30 from that class.
SimpleJack SimpleJack

2012/10/28

#
thanks
You need to login to post a reply.