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

2016/3/21

Stop the music looping through levels.

mateo3012 mateo3012

2016/3/21

#
Hi, I have a piece of code that is misbehaving and I do not know why. So here is what I expect it to do; I except it to pause after I move from level 2 to level 3. But instead it just continues playing on, as well as starting a new instance of that song. So here is the code for the so called button of mine that causes the problems.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Music here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Music extends Actor
{
    public GreenfootSound myMusic = new GreenfootSound("backGroundMusic.mp3");
    boolean firstTurn = true;
    
    /**
     * Act - do whatever the Music wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(firstTurn)
        {
            myMusic.playLoop();
            firstTurn= false;
            
        }
        
        if(Greenfoot.mouseClicked(this))
        {
            if(myMusic.isPlaying())
           {
              myMusic.pause();
              setImage("play.png");
           } else
           {
               myMusic.playLoop();
               setImage("pause.png");
            }
            
        
        }
    }    
}
If there is any other code you need in order to understand a bit more about my game please let me know.
Super_Hippo Super_Hippo

2016/3/21

#
You are not showing how you want to pause/stop the music when changing the world. You need something like this when changing the world:
((Music) (getObjects(Music.class).get(0))).myMusic.stop();
You need to login to post a reply.