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

2014/9/6

Help please im new at this and need help with music in my world

1
2
DragonGirl DragonGirl

2014/9/6

#
My Intro to Programming class requires me to make a cool game by just looking up how to program things. I've got most of it figured out but the sound control is confusing me. I've figured out how to add sounds but when the object is removed the sound won't stop playing and if i add a sound to the world it plays before iI press the run button. I also would like to add an end game screen that ends the scenario
danpost danpost

2014/9/6

#
Instead of using 'Greenfoot.playSound' to play the sound, create GreenfootSound objects and use the method of its class to control them.
DragonGirl DragonGirl

2014/9/6

#
ok hold on, remember im new here how would i do this? and thank you for your help
danpost danpost

2014/9/6

#
Please see the GreenfootSound API documentation for what is required to construct a GreenfootSound object and to see what methods are available to use on the object..
DragonGirl DragonGirl

2014/9/7

#
This is what i have now
1
2
3
public void GreenfootSound(java.lang.String filename)
    {
    }
if i'm being annoying im sorry i just learn better talking to people then reading from a website or book
DragonGirl DragonGirl

2014/9/7

#
so what do i do from here
danpost danpost

2014/9/7

#
You cannot do much from somewhere you should not be. You create a GreenfootSound object by using the 'new' keyword: 'new GreenfootSound(/* name of file containing the sound in double quotes */)'. If I put a sound file called "explosion.wav" in the 'sounds' folder of a project, then to play that sound I would use:
1
new GreenfootSound("explosion.wav").play();
However, coding it this way does not give me any control over the sound. I cannot stop, pause or change the volume of it. For that, I would need a reference to the sound object created:
1
2
GreenfootSound explosionSound = new GreenfootSound("explosion.wav");
explosionSound.play();
Now, I can save 'explosionSound' in a field, so that I can do things with it later (like stopping the sound before it finishes).
DragonGirl DragonGirl

2014/9/7

#
1
2
3
4
5
public void GreenfootSound(java.lang.String filename)
    {
        GreenfootSound  "WoW Theme.wav" = new GreenfootSound("WoW Theme.wav");
        ("WoW Theme.wav").play();
    }
Like this? (WoW Theme) is the sound I'm trying to play
danpost danpost

2014/9/7

#
That could not possibly compile. Neither line 3 nor line 4 could possibly compile without an exception being thrown. A variable is assigned a value like as in the following (in pseudo-code): variableType variableName = expression_or_value; 'variableType' can be any primitive type (int, float, boolean, long, byte, char, etc.) or a class name (String, Color, Font, Actor, World, etc.). 'variableName' can be any name that follows the rules given at the bottom of this page. Your expression to the right of the equal sign on line 3 looks good. Your variable name will not fly. The method signature is outrageous. Method names should give some indication as to what the method does; the first word in the name should be a verb. The argument 'filename' is not used in the method anywhere -- so there is no need to have the argument at all. If you got 'GreenfootSound(java.lang.String filename)' from the API, then you were looking at the constructor signature; and you used it properly at the end of line 3. I am not sure what you were trying to do with line 1 (or why you wanted to create a method here).
K_wow K_wow

2014/9/7

#
Try something like this:
1
2
3
4
5
public void newSound() 
    GreenfootSound  wowTheme = new GreenfootSound("WoW Theme.wav"); 
    wowTheme.play(); 
As for the differences: Note that at the first line (public void newSound()), there is nothing inside the brackets. That is only necessary when you want to input a value that your method can use. Also note that the variable name (wowTheme) does not have quotation marks on it, as variable names are not strings (meaning they are not text, they are code).
danpost danpost

2014/9/7

#
I do not see any reason to create a method for something that can be written in one line:
1
new GreenfootSound("WoW Theme.wav").play();
Again, the problem with this and the method above is that you have no control over a sound that you do not get a reference to (in the method, the reference is lost when the method is done executing). A GreenfootSound field is required to hold the sound until such time as you want to pause it or stop it. The following, when placed in your world class, will have the sound start when the 'Run' button is clicked and stop when the 'Pause' button is clicked. The first line holds a reference to the GreenfootSound object in the constant class field called 'theme' which can be used throughout the class.
1
2
3
4
5
6
7
8
9
10
11
static final GreenfootSound theme = new GreenfootSound("WoW Theme.wav");
 
public void started()
{
    theme.playLoop();
}
 
public void stopped()
{
    theme.pause();
}
DragonGirl DragonGirl

2014/9/7

#
Awesome thanks a lot guys is it the same for objects not just the world they are in?
danpost danpost

2014/9/7

#
DragonGirl wrote...
Awesome thanks a lot guys is it the same for objects not just the world they are in?
I am not exactly sure what you mean by this. Please elaborate.
DragonGirl DragonGirl

2014/9/7

#
i have 3 actors a leaf a spider and a wombat the wombat needs to eat 8 leaves to win and has to avoid the spider or it loses i want the wombat to plays a song when it eats ALL the leaves and end the scenario and if the spider gets to the wombat before he gets all the leaves the the spider wins and for now i have the spider playing a theme but it doesn't end the game or stop when i press the pause button
K_wow K_wow

2014/9/7

#
DragonGirl wrote...
i have 3 actors a leaf a spider and a wombat the wombat needs to eat 8 leaves to win and has to avoid the spider or it loses i want the wombat to plays a song when it eats ALL the leaves and end the scenario and if the spider gets to the wombat before he gets all the leaves the the spider wins and for now i have the spider playing a theme but it doesn't end the game or stop when i press the pause button
An actor would play a sound the same way as the world, yes.
There are more replies on the next page.
1
2