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

2012/10/18

Help with delay on backgroundmusic

Tezuka Tezuka

2012/10/18

#
Hi I need help with delay on backgroundmusic. I want to add 1 s delay after the background music. I have a Greenfoot method called music.playLoop and i want after the end of the song it will have a delay of 1s then play the song again. any suggestions?
danpost danpost

2012/10/18

#
Do not use 'playLoop', but use 'play' instead. Then in the world class, add an int variable 'private int musicDelay;' and in the world act:
if (musicDelay == 0 && !music.isPlaying()) musicDelay = 200;
if (musicDelay > 0)
{
    musicDelay--;
    if (musicDelay == 0) music.play();
}
all occurances of 'music' need replaced with the name of your GreenfootSound object and the '200' can be adjusted to approximate one second.
Tezuka Tezuka

2012/10/18

#
thx for fast reply! but how can I stop the music? I want to push a Key then the music plays and then another key and the music stops help pls
danpost danpost

2012/10/19

#
Things could get complicated and a bit dicey, especially if you are using other keystrokes to perform other actions. Better would be to create a speaker icon with two different images (one with a red cross and one without). Keep track of the state (image) and inform the world when clicked on (to turn on and off the music) by calling a world method that would set the new state. You will need a boolean variable in the world to track the state of the switch.
// instance world variable
boolean musicSwitchOn = true;
// method to switch state
public void musicSwitch()
{
    musicSwitchOn = !musicSwitchOn;
    if (musicSwitchOn) musicDelay = 1; else music.stop();
}

private void checkMusic()
{
    if (!musicSwitchOn) return;
    if (musicDelay == 0 && !music.isPlaying()) musicDelay = 200;
    if (musicDelay > 0)
    {
        musicDelay--;
        if (musicDelay == 0) music.play();
    }
}
Tezuka Tezuka

2012/10/19

#
Hi danpost I am not quite sure what u meant , but what I want is a method to stop the backgrounds music loop. For example when i press down a key its stop playing the music.
danpost danpost

2012/10/19

#
If you insist on using a keystroke to control the background music then the following code will toggle on and off the music with the 'p' key:
// instance variables
boolean pDown = false;
boolean musicSwitchOn = true;
// musicSwitch method
public void musicSwitch()
{
    if !pDown && Greenfoot.isKeyDown("p"))
    {
        pDown = true;
        musicSwitchOn = !musicSwitchOn;
        if (musicSwitchOn) musicDelay = 1; else music.stop();
    }
    if (pDown && !Greenfoot.isKeyDown("p")) pDown = false;
}
// with the same checkMusic method above
// in the act method
musicSwitch();
checkMusic();
Tezuka Tezuka

2012/10/19

#
thnx Danpost it worked great! Now i have a new question mby u could help? I want to keep track of all my keys and afterward I am able to see wich keys that I have pressed in what order thnx in advance
danpost danpost

2012/10/19

#
The following will create a list of keys pressed concatenated in a String variable. Keypresses are seperated from each other by colons (" : ").
// instance variable (in the world class)
String keyOrder = ":"; // to hold the keys pressed
// method recordKey() (in the world class)
// to save a record of keys pressed
private void recordKey()
{
    String key = Greenfoot.getKey();
    if (key != null) keyOrder += key + ":";
}
// in the act() method (in the world class)
// to call the method above
recordKey();
// method stopped() (in the world class)
// to display the keys pressed
public void stopped()
{
    System.out.println(keyOrder);
}
This will only save those keys that have names in Greenfoot. See the API documentation of the 'Greenfoot' class for the list of keys that can be recorded.
Tezuka Tezuka

2012/10/20

#
I tried your code but i changed the ":" to "aKey" and put the method recordKey in the act Method but it didnt worked. when i pause my scenarie all i get is the text aKey
Tezuka Tezuka

2012/10/20

#
nvm it worked now but i dont really understand that ":" and +=
danpost danpost

2012/10/20

#
// Saying
keyOrder += key + ":";
// means the same thing as
keyOrder = keyOrder + key + ":";
It is just a short way of saying to add what is on the right to the value of what is on the left and store it in what is on the left. I put the ":" symbol between each recorded key. Since some keys are returned as Strings of length greater than one, I felt it best to prevent confusion between, say... "HellospaceWorld" and ":H:e:l:l:o:space:W:o:r:l:d:". Notice how without the colons, your program may interperate the former as H e l l o s p a c e W o r l d instead of H e l l o W o r l d If it only printable characters that you are recording, you can drop the colons and convert 'space' to the actual character for the String. You will have to catch it in the recordKey() method. Same with the editing keys. Something like the following:
private void recordKey()
{
    String key = Greenfoot.getKey();
    if (key == null) return;
    if ("space".equals(key)) key = " ";
    if ("backspace".equals(key) && keyOrder.length() > 0)  keyOrder = keyOrder.substring(0, keyOrder.length() - 1);
    if (key.length() == 1) keyOrder += key;
}
kevv kevv

2012/10/20

#
Hi danpost, I would like to use the following code, but it does not play any sound.
if (musicDelay == 0 && !music.isPlaying()) musicDelay = 200;  
if (musicDelay > 0)  
{  
    musicDelay--;  
    if (musicDelay == 0) music.play();  
} 
I have set the music
    GreenfootSound music = new GreenfootSound("mymusicfile");  
My version of the code:
    private void playLoop()
    {
        if (i == 0 && !music.isPlaying()) 
        {
            i = 10;  
        }
        if (i > 0)  
        {  
            i--;  
            if (i == 0) 
            {
                music.play();  
            }
        } 
    }
If I replace if (i > 0) with while it plays once, but I need it to loop.
danpost danpost

2012/10/20

#
You just need to call this method from the act method without condition
//add to act method in world
playLoop();
You need to login to post a reply.