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

2014/4/26

Background music for multiple levels

Rubric94 Rubric94

2014/4/26

#
i just cant quite seem to get this working, i have a title and 3 levels and i have 4 tracks, one for each, i just cant seem to get them working, when i go to the next level, the previouse level music has to end and the new ones begin, here are my tracks.
private GreenfootSound Title = new GreenfootSound ("Title.wav");
    private GreenfootSound Lvl1 = new GreenfootSound ("Lvl1.wav");
    private GreenfootSound Lvl2 = new GreenfootSound ("Lvl2.wav");
    private GreenfootSound Lvl3 = new GreenfootSound ("Lvl3.wav");
danpost danpost

2014/4/26

#
Sound fields, as with any type fields that have the same constant value for all objects of a class, can be made 'static'. What this does is it makes the fields 'class fields'. A field that is shared among all objects of the class (as opposed to 'instance fields', which each object will be assigned a unique individual field that can potentially hold different values). 'static' fields, when made public, can be accessed from anywhere within the project by using the following fomat: ClassName.StaticFieldName where 'ClassName' is the name of the class that the 'public static' field is located. And since these sound fields are constant, they can be made 'final' as well:
public static final GreenfootSound 
    Title = new GreenfootSound("Title.wav"),
    Lvl1 = new GreenfootSound("Lvl1.wav"),
    Lvl2 = new GreenfootSound("Lvl2.wav"),
    Lvl3 = new GreenfootSound("Lvl3.wav");
Now, when changing levels, if the current level sound file is playing, stop it; then, start the new level sound and change levels.
if (ClassName.Lvl2.isPlaying()) ClassName.Lvl2.stop();
ClassName.Lvl3.playLoop();
// code to start new level 3
Rubric94 Rubric94

2014/4/26

#
so, if i put the
public static final GreenfootSound   
    Title = new GreenfootSound("Title.wav"),  
    Lvl1 = new GreenfootSound("Lvl1.wav"),  
    Lvl2 = new GreenfootSound("Lvl2.wav"),  
    Lvl3 = new GreenfootSound("Lvl3.wav");  
in universe, will the
if (ClassName.Lvl2.isPlaying()) ClassName.Lvl2.stop();  
ClassName.Lvl3.playLoop();  
work if put in my exit class collision in mover?
if(!getObjectsInRange(45, Exit.class).isEmpty())  
      {
        level++;
        
           if(level==2)
           {
               Greenfoot.setWorld(new Level2(spirit));
                spirit.lives--;
           }
           
           if(level==3)
           {
               Greenfoot.setWorld(new Level3(spirit));
                spirit.lives--;
            }
danpost danpost

2014/4/26

#
If you put it in your Universe class, you would use:
if (Universe.Lvl2.isPlaying()) Universe.Lvl2.stop();  
Universe.Lvl3.playLoop();
in the 'if (level==3)' code block and something similar in the 'if (level==2)' code block.
Rubric94 Rubric94

2014/4/26

#
thank you so much, will give it a try and get back to you ^^
Rubric94 Rubric94

2014/4/26

#
sound isnt working, did what you said except for the title and lvl one, because the title and level one arent refferenced in the collision, for the title i put this in the constructor
 /**
     * Constructor for objects of class Title.
     * 
     */
    public Title()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.    
        super();
        cleanup();
        addObject(new StartButton(), 400, 620);
        addObject(new HelpButton(), 576, 445);
        addObject(new StoryButton(), 223, 445);
      Universe.Title.playLoop(); 
    }
and in level 1 i put this
 /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super();
        populateWorld();
       if (Universe.Title.isPlaying()) 
                {
                    Universe.Title.stop();    
                    Universe.Lvl1.playLoop(); 
               }
    }
Rubric94 Rubric94

2014/4/26

#
fixed for everything but title
Rubric94 Rubric94

2014/4/26

#
problem with file itself, will fix
You need to login to post a reply.