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

2016/6/7

Background Music

Camyh15 Camyh15

2016/6/7

#
I am trying to make a game that when I go to the next level, new music starts playing, but when I use the code:
Greenfoot.playSound("SoundFile");
It completely glitches out, so I am using this code:
GreenfootSound Music = new GreenfootSound ("Spag Heddy - Freak it (VIP).mp3");
And then in the act method of the world:
Music.playLoop();
Do you know how to access the 'Music' code from other worlds/objects? Thanks!
SPower SPower

2016/6/7

#
Simply store the sound object in an instance variable and create a getter for it (a getter is simply a method that returns the variable), e.g.:
private GreenfootSound music = new GreenfootSound(...);

public GreenfootSound getBackgroundMusic()
{
    return music;
}
Also, it is advisable not to start variable names with an Uppercase letter.
Camyh15 Camyh15

2016/6/7

#
Thank you, but I still can't get my main character to the 'music' code (it just says "cannot find symbol - variable music"). Do you need anything else in the code for it to recognize it or does it need different code completely? This is my world code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
    GreenfootSound music = new GreenfootSound ("Spag Heddy - Freak it (VIP).mp3");
    HealthBar healthbar = new HealthBar();
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    
    public GreenfootSound getBackgroundMusic()
    {
        return music;
    }
    
    public void act()
    {
        music.playLoop();
        music.setVolume(50);
        if(Greenfoot.isKeyDown("enter"))
        {
            music.pause();
            Greenfoot.setWorld(new Level1());
        }
    }
And this is my main characters code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gru here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gru extends Actor
{
    int newLevel =0;
    /**
     * Act - do whatever the Gru wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("right"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            move(-5);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            move(10);
        }
        if(Greenfoot.isKeyDown("A"))
        {
            move(-10);
        }
        setRotation(90);
        if(Greenfoot.isKeyDown("up"))
        {
            move(-5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("W"))
        {
            move(-10);
        }
        if(Greenfoot.isKeyDown("S"))
        {
            move(10);
        }
        setRotation(0);
        if(isTouching(Minion.class))
        {
            World myWorld = getWorld();
            removeTouching(Minion.class);
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
            newLevel++;
            Greenfoot.playSound("Collect.mp3");
            myWorld.addObject(new Explosion(),getX(),getY());
            if(newLevel >= 10)
            {
                Greenfoot.setWorld(new Next1());
            }
        }
    }    
}
Is it something to do with 'getting' the music in the actor?
danpost danpost

2016/6/7

#
You can use the following in the actor class to get a reference to the music there:
GreenfootSound music = ((Level1)getWorld()).music;
SPower SPower

2016/6/7

#
If you're using danpost's method, then you wouldn't need the getter for music at all; I supplied that in my solution because there music was a private variable (which it isn't in this case).
Camyh15 Camyh15

2016/6/8

#
Thanks, it's almost working, it's just that when I try and play the game, the greenfoot terminal window pops up and says: java.lang.NullPointerException at Gru.<init>(Gru.java:11) at Level1.prepare(Level1.java:50) at Level1.<init>(Level1.java:21) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at greenfoot.core.Simulation.newInstance(Simulation.java:607) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:445) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) And this is my code for my actor:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gru here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gru extends Actor
{
    GreenfootSound music = ((Level1)getWorld()).music;
    int newLevel =0;
    /**
     * Act - do whatever the Gru wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("right"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            move(-5);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            move(10);
        }
        if(Greenfoot.isKeyDown("A"))
        {
            move(-10);
        }
        setRotation(90);
        if(Greenfoot.isKeyDown("up"))
        {
            move(-5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("W"))
        {
            move(-10);
        }
        if(Greenfoot.isKeyDown("S"))
        {
            move(10);
        }
        setRotation(0);
        if(isTouching(Minion.class))
        {
            World myWorld = getWorld();
            removeTouching(Minion.class);
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
            newLevel++;
            Greenfoot.playSound("Collect.mp3");
            myWorld.addObject(new Explosion(),getX(),getY());
            if(newLevel >= 10)
            {
                Greenfoot.setWorld(new Next1());
                music.pause();
            }
        }
    }    
}
This is the code for my world, just in case you need it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
    GreenfootSound music = new GreenfootSound ("Spag Heddy - Freak it (VIP).mp3");
    HealthBar healthbar = new HealthBar();
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    
    public void act()
    {
        music.playLoop();
        music.setVolume(50);
        if(Greenfoot.isKeyDown("enter"))
        {
            music.pause();
            Greenfoot.setWorld(new Level1());
        }
    }

    public HealthBar getHealthBar()
    {
        return healthbar;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Counter counter = new Counter();
        addObject(counter,492,286);
        counter.setLocation(75,42);
        addObject(healthbar, 387, 45);
        Gru gru = new Gru();
        addObject(gru,704,107);
        Bad bad = new Bad();
        addObject(bad,91,165);
        Bad bad2 = new Bad();
        addObject(bad2,96,517);
        Bad bad3 = new Bad();
        addObject(bad3,516,319);
        bad3.setLocation(568,314);
        Minion minion = new Minion();
        addObject(minion,273,103);
        Minion minion2 = new Minion();
        addObject(minion2,382,125);
        Minion minion3 = new Minion();
        addObject(minion3,277,250);
        Minion minion4 = new Minion();
        addObject(minion4,137,300);
        Minion minion5 = new Minion();
        addObject(minion5,326,440);
        Minion minion6 = new Minion();
        addObject(minion6,515,566);
        Minion minion7 = new Minion();
        addObject(minion7,718,517);
        Minion minion8 = new Minion();
        addObject(minion8,705,336);
        Minion minion9 = new Minion();
        addObject(minion9,449,356);
        Minion minion10 = new Minion();
        addObject(minion10,600,164);
        minion2.setLocation(227,526);
        minion5.setLocation(278,354);
        minion3.setLocation(395,206);
    }
}
I am not quite sure what this means so I don't know how to fix it. This only occurs when I have put in the code because when I take out the code, everything works again. Do I need to do something else, or have I put the code in the wrong place?
danpost danpost

2016/6/8

#
Yes. You have put the code in the wrong place. Calling 'getWorld' within the constructor will always return a 'null' value (the actor is not in any world while it is being created and can only be placed into a world after the execution of the constructor. You can remove line 11 from the Gru class and change line 64 to:
((Level1)getWorld()).music.pause();
Actually, it can be added to a world during the execution of the constructor; but, the World object would need to be accessible to the actor (either by passing it to the constructor as an argument or by having a reference to the world stored in an accessible class field somewhere).
Camyh15 Camyh15

2016/6/8

#
Thank you!!!! This has really helped, but is there any way to get my start screen's music that I paused, start playing again when I go to the next level in the the worlds code?
danpost danpost

2016/6/8

#
If you do not mind the music starting back at the beginning, you could just create a new GreenfootSound instance for it. If you insist that the music continue from where it was paused at, then there are a couple of things you could do -- make the 'music' field a class field by adding the 'static' modifier to the declaration line of the field or pass the Level1 world or the GreenfootSound object to the Next1 world. It would probably be easiest to use the 'static' modifier, as then, you can access the 'music' field from anywhere outside the Level1 class with:
GreenfootSound music = Level1.music;
Camyh15 Camyh15

2016/6/9

#
Thank you soooooo much!!!!!
You need to login to post a reply.