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

2014/3/30

Sound plays again despite being told not too

trias95 trias95

2014/3/30

#
Hey so ive been scouring the web for a while now and i cant seem to find the answer to this problem. I ahve multiple worlds. one of which is a start screen that loads when you enter the game. This is all fine. however from the start screen you can play or you can go tot he help menu. when you go to the help menu and then return to the start screen world, the music that is set to play simply plays again? My assumption is that the instance of title.mp3 that was originally created is now no longer accessible from the new world instance of the start screen and that is why the (if not playing then play" method isnt working here as it doesnt recognise the first instance of the sound so then it just plays a second one. How can i make it recognise that that first instance is still there and therefore not play a new one?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start extends World
{
      GreenfootSound title = new GreenfootSound("title.mp3");
    
    /**
     * Constructor for objects of class Start.
     * 
     */
    public Start()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 

        prepare();
        
         if( !title.isPlaying())
         {
             title.setVolume(50);
             title.play();
         }
        
        
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        startscreen startscreen = new startscreen();
        addObject(startscreen, 418, 331);
        startscreen.setLocation(400, 300);
        StartButton startbutton = new StartButton();
        addObject(startbutton, 238, 376);
        startbutton.setLocation(244, 338);
        howToPlay howtoplay = new howToPlay();
        addObject(howtoplay, 252, 480);
        howtoplay.setLocation(246, 477);

       
        
           
        
    }
    public void act()  
    {  
      
    }
}
thats my start screen code. the problem is as described above. when leaving and returning to this screen, a new instance of the audio is created.
lordhershey lordhershey

2014/3/30

#
if you are getting a new audio instance that is playing you might try this:
public class Start extends World
{
      static GreenfootSound title = null;

      static {
            title = new GreenfootSound("title.mp3");
      }
...
}
This will only be initialized once when the class is loaded - inside the static block with the curly braces.
trias95 trias95

2014/3/30

#
You. are. my. hero.
trias95 trias95

2014/3/30

#
Is there a way to interact with that instance of audio from an actor class? i have a button and i'd like to stop the start screen music playing when i enter the game eg on that button click?
trias95 trias95

2014/3/30

#
Nevermind, ive got that nailed by putting
Start.title.stop();
on the world that loads next. Thank you!
You need to login to post a reply.