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

2019/5/14

SOUND DURATION

gdrgnxxi gdrgnxxi

2019/5/14

#
i want to put different sounds per world. ex(on start world, mechanics world, real game world) is there a way that a certian music plays while the player is at that certain world then it will change when it changes to other world? LIKE DIFFERENT MUSIC FOR DIFF WORLD and it will just change depending on where the player is at the moment. 1. Start Game - music 2. Level 1 - another music 3. Level 2 - another music 4. Game over world - another music like that.
gdrgnxxi gdrgnxxi

2019/5/14

#
It is a background music btw.
danpost danpost

2019/5/14

#
gdrgnxxi wrote...
is there a way that a certian music plays while the player is at that certain world then it will change when it changes to other world? LIKE DIFFERENT MUSIC FOR DIFF WORLD and it will just change depending on where the player is at the moment.
Yes, but you must code it to change with the changing of worlds. It will not change on its own, you know. I did write a support class for controlling the background music for different worlds. You can find it in my support classes collection and it is named BGMusic.
gdrgnxxi gdrgnxxi

2019/5/15

#
Hello so i tried using the stop() method but it doesnt work?? Here is the StartGame World where i first put the background music. I want it to stop when I click the play button.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StartGame here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartGame extends World
{
    public GreenfootSound start = new GreenfootSound("Background Music.mp3");
    /**
     * Constructor for objects of class StartGame.
     * 
     */
    public StartGame()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    
    public void act(){
        start.play();
        //stopMusic();
    }
    
    /*public void stopMusic(){
         if(Greenfoot.mouseClicked(Play.class)){
             start.stop();
    }
} */
    /**
     * 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 start = new Start();
        addObject(start,389,363);
        start.setLocation(412,346);
        Help help = new Help();
        addObject(help,421,407);
        help.setLocation(412,395);
    }
}
gdrgnxxi gdrgnxxi

2019/5/15

#
Here is the code for the Button (Play actor)

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Play extends Actor
{
    public Play()
    {
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 2;
        int myNewWidth = (int)image.getWidth() / 2;
        image.scale(myNewWidth, myNewHeight);
    }
    
    public GreenfootSound start = new GreenfootSound("Background Music.mp3");
    public void act() 
    {
        click();      
        start.stop();
    }
    
      private void click()
   {
      if(Greenfoot.mouseClicked(this)){
          Greenfoot.setWorld(new RealGame());
        }
}
}
danpost danpost

2019/5/15

#
I will give my solving suggestion at the end; first, however, I would like to inform you of some of the things I see wrong with the given codes. A big issue is that the object created at line 15 in your Play class is never playing and line 19 in that class will try to stop it and not the one created in your StartGame class which is playing. Next, even if start in your Play class was playing, it would stop instantly because the act method tells it to. That is, the very first time act is executed for the Play object, line 19 would stop it. Basically, what this means is that the line is either misplaced or the command to stop it should be restricted by some if condition. In your StartGame class, I do not see where a Play object is created and added to the world. Best would probably be to have your StartGame object check for the click on the Play button. This accomplishes two things; it gives the world the responsibility to do "world" things and puts the code where the start object is. So, remove lines 14 thru 27 in the Play class; create a field for the button and check for clicks on it and perform actions upon clicks on it in your StartGame class act method.
You need to login to post a reply.