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

2014/11/2

sound problem

1
2
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
If I'm calling sounds.PlayMusic() inside Player then does:
Where 'sounds' is declared outside the method.
mean I would be declaring it inside the actor Player his code but not inside any method? So at the top of my code but at least outside any method? Because I just tried and it still keeps calling music.play(); for every time it goes through the loop. I don't think the way I call it really matters when the statement music.isPlaying() would return a true/false response if music is playing or not. Because the music is certainly playing. It just keeps getting started. So if I could find a way to keep it out of the part where it calls music.play() using the music.isPlaying() method it should be fine... right? Or am I getting to hung up on one method?....
danpost danpost

2014/11/2

#
The problem was that each time you create a new Sounds object (every act cycle) you, in turn, create a new instance of the GreenfootSound object called 'music'. Each is a separate instance and is not playing when created and therefore starts playing. You only want one instance of the music, so you only want one Sounds object created. To maintain any object created that is not placed into the world, it must be saved in a field that either belongs to a class or to another object. Just like you maintain your GreenfootSound object, 'music', as an instance field belonging to a Sounds object, the Sounds object needs to be maintained elsewhere.
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
Aand I failed ... badly... I read what you wrote, then thought about it and prety much copy-pasted it into my code. Obvious idiot that I am, While having
1
Sounds sounds = new Sounds();
declared outside of my methods I then placed your code exactly as you wrote it into mine. So like this:
1
2
sounds = new Sounds();
sounds.PlayMusic();
Like I said the obvious idiot that I can be sometimes I completely forgot that creating the line
1
sounds = new sounds();
would just create another instance of the sounds class which is exactly what you told me not to do. Anyway, Just taking that out made it work perfectly fine now. Thanks for the help danpost.
danpost danpost

2014/11/3

#
It, to me, seems a bit much to create a class called Sounds just to play a particular sound. As you have it, you have the Sounds class, plus you have a reference to the object created from it. And this class has a reference to the new music object with a method to keep it running that must be regularly called. It would seem that instead of the Sounds object reference, you could just have the music object reference in the class of the player and instead of calling the PlayMusic method, just do this;
1
if (!music.isPlaying()) music.play();
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Well I had a number of other methods in the Sounds class, but those just weren't interesting for the music. So I had a sound for the playing dying, I had one for the player jumping, I had one for the enemy dying, and so on. The point was that none of those had any effect on the music i was trying to play, but they did create reason enough to me to create a seperate class Sounds so that I had an overvieuw of the sounds I was calling in certain events. By the way, I noticed that when making a new world it would keep running the music, but start a second instant of music at the same time. Obviously hard to counter since I can't really create music only once then pass it on. Or so I thought. Turns out I can make the original creation of the music vairable:
1
GreenfootSound music = new GreenfootSound("music.wav");
into a public static. It may not be great coding seen again I'm using a static, but at least this way it won't start music multiple times. Because that really kept buggin me no matter what. Anyway this was one solution. If you have another one I'm more then interested in seeïng your workaround.
danpost danpost

2014/11/3

#
Well, I have written a more versatile class that would be a big brother to your Sounds class. It is in my BGMusic Actor Class Demo scenario. Not only do you not need to add the actor created into the world, you do not even need to keep a reference to the BGMusic object created (you can just create another object and execute methods on it to control the current music; and, this can be done from any world, as well). You will need to include the image for the class into your images folder, however (or remove any reference to it within its class code). It is a clickable switch to switch the music on and off. It has a basic speaker image.
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
I see, I'll take a look at that straigth away. While I'm doing that maybe you can give me an answere to the following? I'm trying to use World.stopped() which should return true if the world is paused at the moment for as far as I get it. (this seems simular to my music.isPlaying -_-). Anyway, It gives me the usual non-static, static error. However seen World is an abstract object (an error I get when trying to make a reference) I can't create a reference like World music = new World();. So knowing that am I just misunderstanding how the stopped-method works?
danpost danpost

2014/11/3

#
BTW, the individual sounds (like for player dying, jumping, etc.) are very short-lived sounds and do not require any control. They are best placed in the class of the object that uses them (the class of the player for dying and jumping). They can also be created on the fly without needing references to them. For an example:
1
(new GreenfootSound("jump.wav")).play();
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
I can get why that would be preferable for when creating larger codes yes. However seen I do this part as a personal project and part for an assignment I kindoff want to keep it understandable for every person first reading the code. Although what you suggested might just have as much understandability as what I'm doing... I'll think about which option I'm going to pick. By the way, seen you posted your code of BGMusic on greenfoot does that mean you're okay with me saving it on my pc for future reference? Because it really is one hell of a program you made.
danpost danpost

2014/11/3

#
First, the 'stopped' method is not a static method. This means you must supply a world object to execute the method on (which world object is the method being called on). Second, the return type of the 'stopped' method is void, meaning that it will not return a true/false value. Finally, the 'stopped' method is usually not called explicitly. There may be instances where you may want to call it; however, it is mainly called internally by greenfoot when the 'Pause' button is clicked or a 'Greenfoot.stop()' method is executed (when the scenario goes from a running state to a stopped state). Anyway, it is nothing like the 'isPlaying' method. Thought of the day: how can a program ever return a false value when asked if it is running? (if it is not running, then what is causing the statement to be executed). There is a way to create an Actor or World object that does not belong to any pre-defined subclass even though those classes are abstract. I have only once come across an instance where it was reasonable to do so with a World object, using something like this:
1
World world = new World(600, 600, 1){};
I have, on the other hand, found multiple uses for doing so with Actor objects.
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
I was trying to use it to stop my music when the player would select a new level from the menu I created. Although the music gets paused when calling the menu it starts running as soon as the next level is loaded. Even though the next level is paused using the greenfoot.stop-method. Anyway, seen I think I've gone way overboard compared to what the official assignment looked like (create a game of choise with very few conditions aside from that) I think I will just pass on using the stopped method for now. After all like you said:
how can a program ever return a false value when asked if it is running? (if it is not running, then what is causing the statement to be executed).
So using it might result in a lot of work just to stop the music. By the way, I appreciate the help you gave me these last few weeks man. I'm doing this assignment on my own (compared to others who do it in groups of 2 or even 3) and having you (and others like davmac and Super_Hippo) to help me out (if I really don't get something) was incredibly useful. So thanks again for that and keep up the good work! :)
You need to login to post a reply.
1
2