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

2016/9/27

Randomly playing background music and resetting the position of world objects

1
2
Parte Parte

2016/9/27

#
As the title suggests I would like to be able to have a range of music files play one at a time in a random order continuously until the scenario itself is reset. However I also want all the objects in the world to return to their original positions if the player has been "removed off the world" and if spacebar has been pressed without interrupting the music in the background. Thanks.
danpost danpost

2016/9/27

#
Use 'static' fields for your GreenfootSound background music. Create the various GreenfootSound objects and assign them to the fields where declared.
Parte Parte

2016/9/27

#
danpost wrote...
Use 'static' fields for your GreenfootSound background music. Create the various GreenfootSound objects and assign them to the fields where declared.
Any examples/references I could look at? With this kind of thing I learn and understand it a lot better by taking apart an existing product and see how the pieces work together. Thanks.
danpost danpost

2016/9/27

#
Parte wrote...
Any examples/references I could look at? With this kind of thing I learn and understand it a lot better by taking apart an existing product and see how the pieces work together.
I believe my BgMusic Actor Class Demo scenario exemplifies this.
Parte Parte

2016/9/28

#
danpost wrote...
Parte wrote...
Any examples/references I could look at? With this kind of thing I learn and understand it a lot better by taking apart an existing product and see how the pieces work together.
I believe my BgMusic Actor Class Demo scenario exemplifies this.
Where should I be looking specifically in the demo? I had a look through it and I found myself a bit confused
danpost danpost

2016/9/28

#
The BGMusic class has the 'static GreenfootSound bgm' field which holds the currently selected soundtrack. The class itself can be instantiated (an object of the class can be created) from anywhere, at anytime, to control the current 'bgm'. It also has a static field for the volume. The Background class utilizes the methods of the BGMusic class to get and set the volume as well as setting and controlling the playing of the music. Although all the worlds in the demo look the same, be assured that they are indeed different worlds -- it is just that they are the same type of worlds. I used the class names for the mp3 filenames in the demo to show that this is the case. So when a world is created, the previous music continues to play until notified to change to its specific music (by pressing space). If you a more specific question or are still confused, do not hesitate to respond.
Parte Parte

2016/9/28

#
danpost wrote...
The BGMusic class has the 'static GreenfootSound bgm' field which holds the currently selected soundtrack. The class itself can be instantiated (an object of the class can be created) from anywhere, at anytime, to control the current 'bgm'. It also has a static field for the volume. The Background class utilizes the methods of the BGMusic class to get and set the volume as well as setting and controlling the playing of the music. Although all the worlds in the demo look the same, be assured that they are indeed different worlds -- it is just that they are the same type of worlds. I used the class names for the mp3 filenames in the demo to show that this is the case. So when a world is created, the previous music continues to play until notified to change to its specific music (by pressing space). If you a more specific question or are still confused, do not hesitate to respond.
In your example I don't see anything about randomly selecting the music to play. I wanted music to start when the scenario is running, the music that plays would be randomly selected out of 6 sound files and would randomly select the music to play again once the previously randomly selected music has stopped.
danpost danpost

2016/9/28

#
My scenario does not play the music files randomly, I suggested you look at it to see how music can continue on from one world to the next. That is what you initially ask about. Using a 'static' field to hold the current soundtrack was what it You could create an array to hold the String filenames:
1
2
3
4
5
6
public static final String[] soundFiles =
{
    "ABC.mp3",
    "123.mp3",
    "zyx.mp3"
};
Then in the executable code, you could have this:
1
2
3
4
if (bgm == null || !bgm.isPlaying())
{
    bgm = new GreenfootSound(soundFiles[Greenfoot.getRandomNumber(soundFiles.length)]);
}
I guess the executable code could be placed in a static method:
1
2
3
4
public static void playBGM()
{
    // executable code above
}
so that all worlds could call it from their act methods. For example, if the above static members were in your Background1 class, you could use the following in all your world act methods:
1
Background1.playBGM();
Parte Parte

2016/9/28

#
danpost wrote...
My scenario does not play the music files randomly, I suggested you look at it to see how music can continue on from one world to the next. That is what you initially ask about. Using a 'static' field to hold the current soundtrack was what it You could create an array to hold the String filenames:
1
2
3
4
5
6
public static final String[] soundFiles =
{
    "ABC.mp3",
    "123.mp3",
    "zyx.mp3"
};
Then in the executable code, you could have this:
1
2
3
4
if (bgm == null || !bgm.isPlaying())
{
    bgm = new GreenfootSound(soundFiles[Greenfoot.getRandomNumber(soundFiles.length)]);
}
I guess the executable code could be placed in a static method:
1
2
3
4
public static void playBGM()
{
    // executable code above
}
so that all worlds could call it from their act methods. For example, if the above static members were in your Background1 class, you could use the following in all your world act methods:
1
Background1.playBGM();
Well it's not so much a new level I wanted, what I wanted was how to have randomly selected music start when the scenario runs and to have another music file to be randomly selected to play when the previous music finishes however what I also wanted is that when a specific button is pressed all objects that were in the world at the start of the scenario to return to their original locations in the world while having the music uninterrupted (meaning that the music continuous playing even though all objects of the world have returned to original positions). Unless the code given to me thus far is still applicable.
danpost danpost

2016/9/28

#
Just create a new world instance and set it active:
1
Greenfoot.setWorld(new Background1());
the sound should continue without interruption. Provided you call the 'playBGM' method from all active worlds, the music will play a random selection one after another.
Parte Parte

2016/9/29

#
Not sure if I have miss-interpreted what you have said but here is what I have done:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MusicBackground extends World
{
    public static final String[] soundFiles =
    {
        "Future Club (music).mp3",
        "Hydrogen (music).mp3",
        "Inner Animal (music).mp3",
        "Miami Disco (music).mp3",
        "Vengeance (music).mp3"
         
    };
     
    public static void playBGM()
    {
        if (bgm == null || !bgm.isPlaying())
        {
            bgm = new GreenfootSound(soundFiles[Greenfoot.getRandomNumber(soundFiles.length)]);
        }
    }
     
    /**
     * Constructor for objects of class MusicBackground.
     *
     */
    public MusicBackground()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
    }
}
also bgm cannot find variable, what should the variable be?
Super_Hippo Super_Hippo

2016/9/29

#
I guess you need to have this line (for example in line 12).
1
private GreenfootSound bgm;
(bgm = background music)
danpost danpost

2016/9/29

#
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.GreenfootSound;
import greenfoot.Greenfoot;
 
public abstract class BgMusic extends Object
{
    public static final String[] soundFiles =
    {
        "Future Club (music).mp3",
        "Hydrogen (music).mp3",
        "Inner Animal (music).mp3",
        "Miami Disco (music).mp3",
        "Vengeance (music).mp3"        
    };
    public static GreenfootSound bgm;
      
    public static void play()
    {
        if (bgm == null || !bgm.isPlaying())
        {
            bgm = new GreenfootSound(soundFiles[Greenfoot.getRandomNumber(soundFiles.length)]);
        }
    }
}
Then use the following in your world act methods:
1
BgMusic.play();
Parte Parte

2016/9/29

#
danpost wrote...
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.GreenfootSound;
import greenfoot.Greenfoot;
 
public abstract class BgMusic extends Object
{
    public static final String[] soundFiles =
    {
        "Future Club (music).mp3",
        "Hydrogen (music).mp3",
        "Inner Animal (music).mp3",
        "Miami Disco (music).mp3",
        "Vengeance (music).mp3"        
    };
    public static GreenfootSound bgm;
      
    public static void play()
    {
        if (bgm == null || !bgm.isPlaying())
        {
            bgm = new GreenfootSound(soundFiles[Greenfoot.getRandomNumber(soundFiles.length)]);
        }
    }
}
Then use the following in your world act methods:
1
BgMusic.play();
I have tried this and have put
1
BgMusic.play();
into my act method in my World class World class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Worldbackground here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Worldbackground extends World
{
     
     
     
    /**
     * Constructor for objects of class Worldbackground.
     *
     */
    public Worldbackground()
    {   
        super(3840, 2160, 1);
        prepare();
 
    }
 
    public void act()
    {
        BgMusic.play();
         
        if (Greenfoot.isKeyDown("r"))
        {
            Greenfoot.setWorld(new Worldbackground());
        }
    }
     
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Movement_Test2 movement_test21 = new Movement_Test2();
        addObject (movement_test21, 100, 100);
        int movement_test21X = movement_test21.getX();
 
        Walltest1 walltest1 = new Walltest1();
        addObject(walltest1,2880,2135);
 
        Walltest1 walltest12 = new Walltest1();
        addObject(walltest12,960,2135);
 
        Walltest1 walltest13 = new Walltest1();
        addObject(walltest13,960,25);
 
        Walltest1 walltest14 = new Walltest1();
        addObject(walltest14,2880,25);
 
        Walltest2 walltest2 = new Walltest2();
        addObject(walltest2,25,540);
 
        Walltest2 walltest22 = new Walltest2();
        addObject(walltest22,25,1620);
 
        Walltest2 walltest23 = new Walltest2();
        addObject(walltest23,3815,1620);
 
        Walltest2 walltest24 = new Walltest2();
        addObject(walltest24,3815,540);
 
         
        Player_Test1 player_test1 = new Player_Test1(movement_test21);
        addObject(player_test1,100,100);
 
        Enemy_Test3 enemy_test3 = new Enemy_Test3();
        addObject(enemy_test3,318,750);
        Enemy_Test3 enemy_test32 = new Enemy_Test3();
        addObject(enemy_test32,529,750);
        Enemy_Test3 enemy_test33 = new Enemy_Test3();
        addObject(enemy_test33,758,745);
        Walltest1 walltest15 = new Walltest1();
        addObject(walltest15,28,613);
         
         
         
        
    }
     
     
}
danpost danpost

2016/9/29

#
I guess I forgot to start the music after setting the 'bgm' field in the 'play' method. Modify the method to this:
1
2
3
4
5
6
7
8
public static void play()
{
    if (bgm == null || !bgm.isPlaying())
    {
        bgm = new GreenfootSound(soundFiles[Greenfoot.getRandomNumber(soundFiles.length)]);
        bgm.play();
    }
}
There are more replies on the next page.
1
2