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

2023/4/3

How do i stop music from playing in when i switch to a certain world?

LuukTheGamer LuukTheGamer

2023/4/3

#
I have 6 worlds but i want the music to stop playing at the 6th one. But how do i do this?
public GreenfootSound testmusic = new GreenfootSound("Testmusic.mp3");
    
    
    
    public Level2_1()
    {    
        super(1280,736, 1);
        testmusic.playLoop();
        
    }
this is my current code for music
danpost danpost

2023/4/3

#
LuukTheGamer wrote...
I have 6 worlds but i want the music to stop playing at the 6th one. But how do i do this? << Code Omitted >> this is my current code for music
Make line 1 static;
public static GreenfootSound testmusic = new GreenfootSound("Testmusic.mp3");
Then you can simply use the following line anywhere in your project:
Level2_1.testmusic.stop();
A static field is called a class field (as opposed to an instance field). It belongs to the class itself, not to the instances created from the class (although, instances of the class can easily make use of the field). It is one value, shared by all instances, as opposed to a field that is duplicated for each instance that can take on separate values for each instance.
LuukTheGamer LuukTheGamer

2023/4/3

#
thanks dan! youre a lifesaver!
TheGoatFighter TheGoatFighter

2023/4/5

#
Danpost 1v1 on me fortnite, luuk the game get of greenfoot and add me on fortnite. Danpost in not a life save he hasnt answered my worries.
You need to login to post a reply.