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

2017/7/29

bg music with multiple levels

AAA355 AAA355

2017/7/29

#
I have looked around but cannot find much about this. my game ha 3 levels and I want it to play different bg music for each level. so far I have the level1 code but for some reason when I go to level 2 it continues to play the music from level 1 at the same time as the music from level 2. level 1 music will pause when I pause the game but only on level 1. level 1 code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class MyWorld here.
* 
 * @author (your name) 
 * @version (a version number or a date)
*/
public class Level1 extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("level1 theme.mp3");

    private int [] [] mazeMap = {
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,2,1,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1},
            {1,0,1,1,0,3,0,0,1,1,0,1,1,1,1,0,1,0,1,0,0,1,1,1},
            {1,0,1,1,0,1,1,0,1,1,0,0,0,3,0,4,1,1,1,1,0,1,1,1},
            {1,0,1,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1},
            {1,0,1,0,1,1,0,1,1,0,1,0,0,1,1,1,1,1,1,0,4,1,1,1},
            {1,0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1},
            {1,8,4,1,1,1,0,1,1,0,0,0,0,0,0,1,1,0,0,3,0,0,0,1},
            {1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1},
            {1,0,1,0,0,1,7,1,1,1,0,1,1,1,1,0,1,0,9,1,1,1,0,1},
            {1,0,1,1,1,1,5,0,0,1,0,0,0,1,1,0,1,1,0,1,0,0,0,1},
            {1,0,1,1,1,1,1,1,0,1,0,1,1,0,1,0,1,1,7,1,0,1,1,1},
            {1,0,1,1,1,1,1,0,0,0,3,0,3,0,0,0,1,0,5,0,0,1,1,1},
            {1,0,0,3,0,0,0,0,1,1,1,1,1,1,1,7,1,0,1,1,0,0,0,1},
            {1,1,4,1,1,1,1,0,0,0,1,1,1,1,1,5,0,0,1,1,1,1,0,6},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        };

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Level1()
    {
        super(1200, 800, 1);
        backgroundMusic.playLoop();
        placeBlocks();
        prepare();
    }
    
    public void started()
    {
        backgroundMusic.playLoop();
    }
    
    public void stopped()
    {
      backgroundMusic.pause();   
    }
level 2 code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class MyWorld here.
* 
 * @author (your name) 
 * @version (a version number or a date)
*/
public class Level2 extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("level2 theme.mp3");

    private int [] [] mazeMap = {
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,2,0,0,0,1,1,0,0,0,1,0,1,1,0,0,0,0,0,0,1,1,1,1},
            {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,1,4,1,1},
            {1,0,0,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,1},
            {1,0,1,0,3,0,0,0,1,0,1,1,0,1,1,0,0,1,1,7,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1,1,0,4,1,1,1,0,0,1,5,0,1,1,1},
            {1,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1},
            {1,0,1,1,1,1,0,0,0,0,3,0,3,0,3,1,1,0,1,1,1,4,1,1},
            {1,0,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,0,0,3,0,0,0,1},
            {1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1},
            {1,0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1},
            {1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,1,1,0,0,1},
            {1,1,1,1,1,1,1,0,0,3,4,0,0,0,1,1,1,1,0,1,1,1,0,1},
            {1,1,0,3,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,1},
            {1,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,6},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        };

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Level2()
    {
        super(1200, 800, 1);
        backgroundMusic.playLoop();
        placeBlocks();
        prepare();
    }
    public void started()
    {
        backgroundMusic.playLoop();
    }
    
    public void stopped()
    {
      backgroundMusic.pause();   
    }
AAA355 AAA355

2017/7/29

#
I also have a problem where each time I die the music keeps going but ads more music from the beginning. (when I die it sets the world to whatever level I am on).
Super_Hippo Super_Hippo

2017/7/29

#
The 'stopped' method is called in the active world when the scenario was paused, not when the world changes. You have to stop the music before setting the new world.
danpost danpost

2017/7/29

#
As an alternative, you can utilize by BGMusic class to control the background music of your scenario. You can find it in my BGMusic Actor Class Demo scenario.
You need to login to post a reply.