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

2018/5/14

Music

Heet_Patel Heet_Patel

2018/5/14

#
Hi guys, actually I had a bit of a thought about the music and I realised that i could actually stop the music when the worlds changed with a help of an actor but then I realised that if Idid that then the next world which also has music wouldn't play the music. Example: I have a start menu with BGM and then I click on the start button and the BGM for the start menu stops. When the world changes the BGM for this world won't play. But then when I go back to the start menu the BGM for the start menu will still play why is this. Codes: Start mnu code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start extends World
{
    /**
     * Constructor for objects of class Start.
     * 
     */
    GreenfootSound backgroundMusic = new GreenfootSound("gamestheme.mp3");
    private Start_too start;
    private Instructions intro;
    public Start()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1); 
        start = new Start_too();
        addObject(start, 500, 250);
        intro = new Instructions();
        addObject(intro, 150, 37);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Start_Button start_button = new Start_Button();
        addObject(start_button,231,250);
        Help help = new Help();
        addObject(help,50,37);
        help.setLocation(38,38);
        start_button.setLocation(499,272);
    }

    public void stopped()
    {
        backgroundMusic.pause();
    }

    public void started()
    {
        backgroundMusic.playLoop();
    }
}
LEVEL Menu code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class LEVEL here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LEVEL extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("gamestheme.mp3");
    /**
     * Constructor for objects of class LEVEL.
     * 
     */
    public LEVEL()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Level_1 level_1 = new Level_1();
        addObject(level_1,168,172);
        level_1.setLocation(177,157);
        Level_1_set level_1_set = new Level_1_set();
        addObject(level_1_set,179,182);
        level_1_set.setLocation(180,156);
        Level_2 level_2 = new Level_2();
        addObject(level_2,463,98);
        level_2.setLocation(463,93);
        LEVEL2 level2 = new LEVEL2();
        addObject(level2,468,209);
        level2.setLocation(463,165);
        level2.setLocation(464,164);
        house house = new house();
        addObject(house,66,61);
        house.setLocation(43,40);
        Level_3 level_3 = new Level_3();
        addObject(level_3,690,107);
        level_3.setLocation(680,97);
        Level_trois level_trois = new Level_trois();
        addObject(level_trois,684,167);
        level_trois.setLocation(677,165);
        level2.setLocation(462,161);
        level_1_set.setLocation(177,206);
        level_trois.setLocation(681,54);
        level_2.setLocation(173,350);
        level2.setLocation(183,421);
        level_3.setLocation(176,501);
        level_trois.setLocation(185,568);
        Icon_for_4 icon_for_4 = new Icon_for_4();
        addObject(icon_for_4,571,140);
        icon_for_4.setLocation(543,132);
        Level_4 level_4 = new Level_4();
        addObject(level_4,554,168);
        level_4.setLocation(550,189);
    }
}
Start Button code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start_Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start_Button extends Startpage
{
    /**
     * Act - do whatever the Start_Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        {
         if (Greenfoot.mouseClicked(this)) 
         {
           ((Start)getWorld()).stopped();
           Greenfoot.setWorld(new LEVEL());
         }
        }
    }    
}
Home button code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class home here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class house extends Startpage
{
    /**
     * Act - do whatever the home wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public house()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 100, image.getHeight() - 100);
        setImage(image);
    }
    public void act() 
    {
        {
         if (Greenfoot.mouseClicked(this)) 
         {
           getWorld().stopped();
           Start start = new Start();
           start.started();
           Greenfoot.setWorld(start);
         }
        }
    }    
}
Sorry for so many codes but please help me Very kindly appreciated.
danpost danpost

2018/5/14

#
Where do you "play" the background music of the LEVEL class?
Heet_Patel Heet_Patel

2018/5/14

#
tnx i forgot
You need to login to post a reply.