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

#
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
MainTheme.stop(); 
to pause
MainTheme.pause(); 
Ajay7heDJ Ajay7heDJ

2021/3/14

#
Uhh there is no error, i put the thing in, but it doesn't work.
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.