So in this game I am making for my comp sci class, I have music that I want to start playing at the start of each level. to try to do this, I created a sound object
I want to figure out how to make an instance of my sound object that can be changed by multiple classes.
I instantiated it in this world, but when the unit calls this method, It switches worlds
So how could i make it so that i can still call the stop methods on the sound object, from a different world than the one it was instantiated in.
If it would help, I can put the project on here aswell.
public class sound extends Actor
{
private GreenfootSound background;
public sound(String name)
{
this.background = new GreenfootSound(name);
}
public void play()
{
background.play();
}
public void stop()
{
background.stop();
}
public void act()
{
}
}public class TitleScreen extends World
{
sound sound1;
/**
* Constructor for objects of class TitleScreen.
*
*/
public TitleScreen()
{
super(30*32, 25*32, 1);
for(int i = 0; i <= 30; i++)
{
addObject(new obstacle("platform2.png"), i*32, 15);
}
for(int i = 0; i <= 30; i++)
{
addObject(new obstacle(), i*32, (25*32)-15);
}
addObject(new tile("StartScreen.png"), 15*32, 12*32);
Greenfoot.start();
this.sound1 = new sound("startScreen.mp3");
}
public void act()
{
if (Greenfoot.isKeyDown("space"))
{
Greenfoot.setWorld(new level1());
sound1.play();
}
}
}public void die()
{
getWorld().removeObject(this);
Greenfoot.setWorld(new deathScreen());
this.isDead = true;
}

