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

2021/3/14

How to add Background Music

Ajay7heDJ Ajay7heDJ

2021/3/14

#
I'm confused, I setup a whole zombie game through a tutorial. But I don't know how to add background music. I have the sound i want to add and I have a getWorld, Player, Weapon, Projectile, etc players but I don't know how to add the music. I tried with another tutorial but the music keeps looping. Can someone help me?
Risen Risen

2021/3/14

#
1
2
3
4
5
6
7
8
9
public class Menu extends World {
 GreenfootSound MainTheme;
 
private void prepare() {
         
MainTheme = new GreenfootSound("MenuMT.wav"); // instead of "MenuMT.wav" use your music file
        MainTheme.setVolume(x); // 0 - 100 (not necessary)
        MainTheme.playLoop(); // for cycle
}
if you want to stop cycle you need to use this
1
MainTheme.stop();
to pause
1
MainTheme.pause();
Ajay7heDJ Ajay7heDJ

2021/3/14

#
Uhh there is no error, i put the thing in, but it doesn't work.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    GreenfootSound MainTheme;
    int count = 0;
    // Spawn speed will be faster if you lower value of spawnSpeed
    int spawnSpeed = 30;
    int randomSpawn;
    public Player mainPlayer = new Player();
    Counter counter = new Counter();
    HealthBar healthbar = new HealthBar();
    WeaponButton weaponButton = new WeaponButton(counter);
    SuperPower superPower = new SuperPower();
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        mainPlayer = new Player(weaponButton, superPower);
        addObject(mainPlayer, getWidth()/2, getHeight()/2);
        addObject(counter, 130, 100);
        addObject(healthbar, mainPlayer.getX() - 5, mainPlayer.getY() - 50);
        addObject(weaponButton, 700, 100);
        addObject(superPower, mainPlayer.getX() + 10, mainPlayer.getY()-80);   
    }
 
    public void act()
    {
        count++;
        spawnZombies();
    }
     
    private void prepare() {
          
        MainTheme = new GreenfootSound("Background Music.mp3"); // instead of "MenuMT.wav" use your music file
        MainTheme.setVolume(100); // 0 - 100 (not necessary)
        MainTheme.playLoop(); // for cycle
    }
    public Player getPlayer()
    {
        return mainPlayer;
    }
 
    public void spawnZombies()
    {
        if (count % spawnSpeed == 0)
        {
            randomSpawn = Greenfoot.getRandomNumber(8);
            switch(randomSpawn){
                case 0 : addObject(new Zombie(mainPlayer,counter), 0, 0);
                case 1 : addObject(new Zombie(mainPlayer,counter), getWidth()/2, 0); break;
                case 2 : addObject(new Zombie(mainPlayer,counter), getWidth(), 0); break;
                case 3 : addObject(new Zombie(mainPlayer,counter), 0, getHeight()/2); break;
                case 4 : addObject(new Zombie(mainPlayer,counter), getWidth(), getHeight()/2); break;
                case 5 : addObject(new Zombie(mainPlayer,counter), 0, getHeight()); break;
                case 6 : addObject(new Zombie(mainPlayer,counter), getWidth()/2, getHeight()); break;
                case 7 : addObject(new Zombie(mainPlayer,counter), getWidth(), getHeight()); break;
            }
        }
    }
}
danpost danpost

2021/3/15

#
The prepare method needs to be called by the MyWorld constructor.
You need to login to post a reply.