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

2011/10/21

General Game Structure/Sound help

Stryker Stryker

2011/10/21

#
New to Greenfoot. I've started creating a Platformer game and decided to implement music. My main question is how I should be organizing my game and where I should be initializing the sound. Currently I have a new world for each level. When a world is completed I will be using the setWorld command to load a new one. When this new world loads it will populate my world according to where I want all my actors placed. This was how I saw my game being created, if you have any suggestions or objections to how I should be running it please tell me. My main question is for what I'm working on right now. I want my sound file to play only when the new world is loaded. For the first (main menu) I suppose I could just run it in the world class. For each level however how am I supposed to STOP the current sound playing? Ie. If MainMenuWorld starts playing "song1" and I click the play button which setWorld's Level1, how do I stop the music from MainMenuWorld? Should I be creating a Music actor that somehow plays the sounds? OR do I have to stop the sound before progressing to each new level? I know I asked a lot of questions but I honestly appreciate any help I can get with this project. Thanks is advanced!
davmac davmac

2011/10/22

#
Probably you just want to start and stop the music from the same place - the world representing the level. I'd recommend against having a music actor mainly because it doesn't give you anything and actors are really meant to be things "in" the world. But: programming can be a bit of an art - there are a lot of ways to achieve the same thing. Do whatever works best for you. Stopping the level sound immediately before progressing to the next level sounds reasonable to me.
Stryker Stryker

2011/10/27

#
If I load the music on each new world creation how am I supposed to call a stop on the music :S ? I understand that the .stop method will stop the sound but how can I call that method from my actor. EXP. MainMenu loads a song If my "Play Button" actor is clicked I want the song to stop, my new world to load, and the current music to stop. My only understanding to make my music stop in MainMenu is to call "myMusicVariable.stop();". I obviously can't call this from my actor as it's unaware of this variable.
ez4u2c ez4u2c

2011/10/27

#
To reference a variable or method in another object see this tutorial: http://www.greenfoot.org/doc/howto-1
Stryker Stryker

2011/10/28

#
Okay, so that does explain a standard getter/setter routine which I had actually just tried before I read your post. When I try this method however, my song doesn't seem to stop AND there's a duplicate version of my song playing :S. I'll try to give you the bones of where I am to make sure I'm doing it correctly. Worlds: MainMenu, Level1 Actors: PlayBTN Currently my music plays before someone even hits the run button (which I would like to change). That aside, my music plays in my constructor for MainMenu. Inside the MainMenu CLASS I have included getter method that returns my music object. Over in my PlayBTN, if it's clicked it does the following: MainMenu MM = new MainMenu(); MM.getMyMusic().stop(); If you require any further explanation to help me I will gladly share! Thanks.
Stryker Stryker

2011/10/28

#
Didn't even realize I was getting an error until after. I get this run-time error after clicking the playBTN: java.lang.NullPointerException at PlayBTN.act(PlayBTN.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
davmac davmac

2011/10/28

#
Difficult to say without seeing your code, but sounds like getMyMusic() is returning null. Perhaps you're never actually setting the variable, or you're re-declaring it as a method-local variable when you try to set it. But also:
MainMenu MM = new MainMenu();
MM.getMyMusic().stop();
^^^ you're creating a new instance of MainMenu, then telling it to stop - surely what you wanted was to get the existing instance and tell it to stop?
Stryker Stryker

2011/10/28

#
Thanks davmac. That was the issue with my duplication of sound. I assumed that the "new" bit would create a new instance but I didn't know how to reference methods from a different class otherwise. This is what I changed it over to: MainMenu MM = (MainMenu)getWorld(); MM.getMyMusic().stop(); As for the null pointer I did manage to fix it but could you perhaps explain why I had to do this in order for it to work. I had been creating my private GreenfootSound variable and NOT assigning a value (song) until the body of my MainMenu constructor. In order to get it to work I had to create my private variable AND assign it outside of the constructor.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MainMenu extends World
{
private GreenfootSound myMusic;

    public MainMenu()
    {    
        super(600, 400, 1);
        
        addObject(new PlayBTN(), 300,300);
        addObject(new TutorialBTN(), 300,350);
        
 myMusic = new GreenfootSound("music1.mp3");
        myMusic.playLoop();
       
    }
    
    public GreenfootSound getMyMusic()
    {
        return myMusic;
    }
    

}
That was the code for my MainMenu. Here is the code from my PlayBTN:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class PlayBTN extends Actor
{

    public void act() 
    {
      if (Greenfoot.mousePressed(this))
        {
            Greenfoot.setWorld(new Level1());
            MainMenu MM = (MainMenu)getWorld();
            MM.getMyMusic().stop();
        }
    }
}
Stryker Stryker

2011/10/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MainMenu extends World
{
private GreenfootSound myMusic = new GreenfootSound("music1.mp3");

    public MainMenu()
    {    
        super(600, 400, 1);
        
        addObject(new PlayBTN(), 300,300);
        addObject(new TutorialBTN(), 300,350);
        myMusic.playLoop();
       
    }
    
    public GreenfootSound getMyMusic()
    {
        return myMusic;
    }
}
This was what I had to do in order to fix my issue. With that now out of the way I was wondering if there was another way to reference my MainMenu class. I ask this because it seems more logical there every time a new Level is loaded, that level with stop the music of the previous one and play the music of the current world. My only issue with my current method is that the getWorld() method can't be used inside of another world. Or at least I'm getting an error when moving my code over from the playBTN to my Level1 world.
davmac davmac

2011/10/28

#
There really shouldn't be a difference - I don't understand why the second version of MainMenu would work if the first one didn't. If you upload the entire scenario I'll have a look.
You need to login to post a reply.