views:

69

answers:

1

Hi!

Sorry to bug twice so quickly, but since people were so kind in their informative responces, I figured it couldnt hurt to ask another question.

The same program i tried to make it rather swanky and have a main screen which allows you to click on a button which leads to a limited options screen. This lets you switch the music on or off. Or at least it should do.

The music running code is in the main file (game.m), under the following:

//Music
[Settings setMusicEnabled:YES];
music = [SPSound soundWithContentsOfFile:@"music.caf"];
channel = [[music createChannel] retain];
channel.loop = YES;
channel.volume = 0.25;
if([Settings musicEnabled]){
    [channel play];
}

I apologize for the strange format, but it is Sparrow framework. basically, the Settings file contains the class methods I am trying to use. If the methods cause YES, the music is on. If it is No, then the music is off.

settings.m

static BOOL isMusicEnabled;
@implementation Settings
+ (BOOL)musicEnabled
{
    return isMusicEnabled;
}
+ (void)setMusicEnabled:(BOOL)value
{
    isMusicEnabled = value;
    NSLog(@"SME? %i", isMusicEnabled);
}
@end

Now, the options file is working and i tested that section. The program is reading that isMusicEnabled is getting a new value, thus musicEnabled is being altered as well, so there should be a change and the music should be switched off.

However, nothing happens. I have tried to use debugger, but I am not very good at it and I dont understand a lot of the information i am given. I do understand that the problem is sending the message from Settings file to the main/Game file.

I would appriciate anyone's help who could enlighten me as to how this could be solved.

A: 

I'm not familiar with Sparrow Framework, but let me make a guess anyway.

[channel play]; starts playing the music in background until the channel is asked to stop playing.

Changing the isMusicEnabled does not trigger any code to stop the currently playing music. When you change the value in Settings, you should inform the channel to stop (most probably by somehow accessing the channel and calling [channel stop].

There's another problem - isMusicEnabled is just a variable in memory, your program will not remember its state between restarts. And Settings are usually supposed to be remembered.

To summarize I see two problems: persisting settings between restarts first and informing about change of settings second. To remember settings I suggest you look into NSUserDefaults class. To inform the channel to stop playing you have couple of options - depending on you skills. Easiest is to simply access the channel variable from within the setMusicEnabled and call stop. Another option would be to use notifications, but for a beginner programmer that is more complicated (look for NSNotificationCenter if interested).

Michal
Thank you very much for the responce. I had wondered about this, but I didnt know exactly where to look to make sure and if i would realise that this was what I am reading. I admit I am still a beginner at programming.I will have a look at the NSNoticifactionsCenter as soon as I can, but are there any recommended sites you would suggest from your own experience?
SKato
You might of course read a book or two, but I suggest you to look into source code of open-source software. There you can see on an existing program how things are done. There are lots of projects on github which you can download and try, or just look at the code via browser.
Michal
Hi. Sorry, I have been through the 2 books I do have, but sadly they dont really help me in this situation. Either that, or i am missing something fundamental with the program and I am a moron (pardon my language). I am having trouble with the simple, setMusicEnabled to call stop. I am a bit lost how I should do that as I have tried, but no luck. I am looking up the NSNotificationCenter, but that looks very hard. I would appreciate any suggestions.
SKato