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

2014/10/21

Start/Stop Music when world is getting loaded.

DerPonyFreund DerPonyFreund

2014/10/21

#
Hey there! I have a little Problem. I tried to make a main Menu, with a sound in the background. The actual game has got another sound in the background, so I need to stop the Main Menu theme when I start the game. The new_Game button starts the game, so I need to stop the Music, when this button is clicked. The problem is, that Greenfoot loads another world, when the Game starts. I tried to detect, when this button is pushed, so I could stop the music, but it just wont work.
import greenfoot.*;



public class Main_menu extends World
{
    //-----------DEFINE SOUNDS-----------
    GreenfootSound bgm2 = new GreenfootSound("wav1.mp3");
    GreenfootSound bgm1 = new GreenfootSound("wav2.mp3");
    //-----------DEFINE OBJECTS-----------
    checkWorld checkWorld = new checkWorld();
    newgame newgame = new newgame();
    Options Options = new Options();
    Credits Credits = new Credits();
    //-----------DEFINE dbb(DebugButton) and cli(Click detected - sign)-----------
    dbb dbb = new dbb();
    cli cli = new cli();
    //-----------SET CURRENT STATE-----------
    boolean Beta =true;
    public Main_menu()
    {    
        super(1100, 700, 1);
        
        //add all main Menu buttons!
        addObject(newgame,550,250);
        addObject(Options,550,450);
        addObject(Credits,84,680);
        //add the Debug-button if Beta=true
        
        if(Beta==true)
        {
        addObject(dbb,997,636);
       }
       //currently disabled function
     
      
      if (worldChanges()==true)
       {   if(Beta==true)
            {
                addObject(cli,900,636);
            }
            bgm1.stop();
            Greenfoot.delay(30);
        }
    }
    
    public void started()
    {
       bgm1.play();
        
    }  
  
    public boolean worldChanges()
    {
        
        if (Greenfoot.mouseClicked("Options")==true)
        {return true;}
        if (Greenfoot.mouseClicked("Credits")==true)
        {   Greenfoot.setWorld(new Cred());
            
            return true;}
        if (Greenfoot.mouseClicked("cli")==true)
        {return true;}
        if (Greenfoot.mouseClicked("newgame")==true)
        {
          return true;
        }
        {return false;}
        
    }
    public void stopped()
    {
        bgm1.pause();
    }
}
Super_Hippo Super_Hippo

2014/10/21

#
First, I don't think that the level changing works at all. I never tried to detect a mouse click on another object, but I am pretty sure that you need to remove all the >>"<< in the parameters for the 'Greenfoot.mouseClicked' method since you want to pass an object and not a string. Then, you should put the detection of clicks into the 'act' method and not in the constructor. What I can say is, it is easier to handle if you put this into the 'act' method of the buttons which are clicked.
DerPonyFreund DerPonyFreund

2014/10/21

#
But how do I detect when a button is pushed or not? I dont know how I should tell the world else, that the world is changing. I tried to do that with the object instead, but I couldn´t get it to work.
danpost danpost

2014/10/21

#
DerPonyFreund wrote...
But how do I detect when a button is pushed or not?
You already have instance fields for each button; however, they should not be named the same as the class that created them (at least not exactly, case-wise). In other words, you should have something like this for lines 12 through 14:
newgame newGame = new newgame();
Options options = new Options();
Credits credits = new Credits();
These actors, 'newGame', 'options' and 'credits', should be checked for clicks on them just as you have tried in your 'worldChanges' method by using those field names (field names do not go inside double-quotes like String objects do). This code should, however, either be in the 'act' method or a method that is called by the act method:
if (Greenfoot.mouseClicked(newGame)) // do whatever
if (Greenfoot.mouseClicked(options)) // do whatever
if (Greenfoot.mouseClicked(credits)) // do whatever
Since all three buttons will result in a new world object being created and set active, you could create a local variable to hold the world to go to before checking for clicks:
World nextWorld = null;
and if a button was found clicked (for the '// do whatever's) set 'nextWorld' to a newly created world of the required type:
if (Greenfoot.mouseClicked(newGame)) nextWorld = new Game();
if (Greenfoot.mouseClicked(options)) nextWorld = new Opts();
if (Greenfoot.mouseClicked(credits)) nextWorld = new Cred();
After this, you can check for a value in 'nextWorld' other than 'null' and set the world active (there is no reason to set booleans and other fields to proceed at a later time from a different method -- as soon as something is found needed to be done, do it; this will simplify your code and make it easier to understand).
if (nextWorld != null)
{
    stopped(); // to stop the music
    Greenfoot.setWorld(nextWorld);
}
Calling 'stopped' does not in itself stop the running of the scenario ('stopped' is called from within greenfoot itself as a response to when the scenario is paused while an world instance of this class is active). You can do other things as well within this final 'if' block; such as passing field values or objects to the new world instance, adjusting the background of the new world instance, or even calling methods on the new world object or on any other object contained within its scope.
DerPonyFreund DerPonyFreund

2014/10/22

#
Okay, thx. I´ll try around a little bit with this.
You need to login to post a reply.