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

2013/10/22

Passing background music to other worlds and controlling.

Tavi Tavi

2013/10/22

#
Hello Everyone ! I'm making a platform game which i will post in little over a week :) , but i'm still working on alot of problems. One of them is the background music. Let's say i have a sound called Trainingsground.. First, im declaring the sound at the top of my world, trainingGround like so :
GreenfootSound trainingGround = new GreenfootSound("TrainingGround.wav");
Then, in my world prepare, i simply tell the world to play the sound in a look like so :
trainingGround.playLoop();
However, i need this sound to keep on playing ( which is does ) when i move to world 2.. But when im leaving world to and moving on to world 3, i need the sound to stop and play a different background tune. I've tried to use the following in the 3rd world :
trainingGround.stop);
. Sadly, the reference is lost and i cannot stop the sound from playing at this point. Can anyone help me out on this :) ? Can't wait to post this to you guys !
danpost danpost

2013/10/22

#
You need to pass the reference from the first world to the second. You can do this by passing an argument in the world constructor call for the second world or you can pass it via a call to a method in the second world. The following shows the method call way.
// creating the new world
World2 w2 = new World2();
w2.setBackgroundMusic(trainingGround);
// in World2 class
// instance field
GreenfootSound trainingGround;
// new method
public void setBackgroundMusic(GreenfootSound music)
{
    trainingGround = music;
}
With this, you should be able to stop the music using 'trainingGround.stop()' in the second world.
Tavi Tavi

2013/10/22

#
Ok Dan :) Seriously.. You're awsome, thanks !
Tavi Tavi

2013/10/30

#
Could you please explain this a little more basic ? I Can't seem to get this to work.. Active code in world 2 :
    GreenfootSound trainingGround;  
    /**
     * Constructor for objects of class Questmap.
     * 
     */
    public Questmap()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1, 1168, 875);
        setBackground("Questmap.png");


       /**
         * GUI
         *
         */
        //Level
        LevelCounter levelcounter = new LevelCounter();
        addObject(levelcounter, 100, 30);
        //XP
        XpBar xpbar = new XpBar();
        addObject(xpbar, 450, 30);
        //Mesos
        MesoCounter mesocounter = new MesoCounter();
        addObject(mesocounter, 100, 80);
        //HP
        LifeBar lifebar = new LifeBar();
        addObject(lifebar, 100, 125);
        //Character
        addCameraFollower(new Rogue(levelcounter, mesocounter, lifebar, xpbar), 0, 0);
        
        //portals
        portalTwo = new Portal(1);
        addObject (portalTwo , 103, 522);
        
        portalThree = new Portal(3);
        addObject (portalThree , 1103, 522);        
        
       setBackgroundMusic((GreenfootSound) trainingGround);
    }
    
    public void setBackgroundMusic(GreenfootSound music)  
    {  
        trainingGround = music;  
        trainingGround.stop();
    }  
I Removed alot of bulk code (platforms etc.) with this i get a nullpointer exception while entering the map, :( it doesnt work atm.
danpost danpost

2013/10/30

#
Remove line 39. Line 45 needs to be moved elsewhere (to wherever the next level is called).
You need to login to post a reply.