OStack程序员社区-中国程序员成长平台

标题: ios - 如何让音乐开/关设置objective-c [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:52
标题: ios - 如何让音乐开/关设置objective-c

我正在 cocos2d 中制作一个简单的游戏,我想制作一个简单的按钮切换来保持声音的开启或关闭。这就是我到目前为止所做的......

在我的.h中:

CCMenuItemImage *sound;
CCMenu *menu2;

在我的init中:

[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey"musicon"];

// Sound Buttons
        sound = [CCMenuItemImage itemWithNormalImage"music.png" selectedImage"music.png" target:self selectorselector(turnOffSound)];
        sound.scale = 1.1;
        menu2 = [CCMenu menuWithItems:sound, nil];
        menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
        [self addChild:menu2];

然后是我的 turnOffSoundturnOnSound 方法:

-(void) turnOnSound {
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey"musicon"];
    [self removeChild:sound cleanup:YES];
    [self removeChild:menu2 cleanup:YES];
    sound = [CCMenuItemImage itemWithNormalImage"music.png" selectedImage"music.png" target:self selectorselector(turnOffSound)];
    sound.scale = 1.1;
    menu2 = [CCMenu menuWithItems:sound, nil];
    menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
    [self addChild:menu2];
}
-(void) turnOffSound {
    [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey"musicon"];
    [self removeChild:sound cleanup:YES];
    [self removeChild:menu2 cleanup:YES];
    sound = [CCMenuItemImage itemWithNormalImage"music-not.png" selectedImage:@"music-not.png" target:self selector:@selector(turnOnSound)];
    sound.scale = 1.1;
    menu2 = [CCMenu menuWithItems:sound, nil];
    menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
    [self addChild:menu2];
}

现在,切换只在这个屏幕(这是标题屏幕)上有效,因为当我转到另一个屏幕,然后返回标题屏幕时,BOOL 再次设置为 true。你知道我怎样才能让它不再重置吗?感谢您的帮助,谢谢!



Best Answer-推荐答案


试试这个,我使用了这个代码并且一直在工作。

@interface AppController : NSObject <UIApplicationDelegate>
{
    bool isMusicOn;
    …
}
@property (nonatomic, assign) bool isMusicOn;
@end

@implementation AppController
@synthesize isMusicOn;


- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
   if([[NSUserDefaults standardUserDefaults] objectForKey:@"isMusicOn"] != nil)
    {
        self.isMusicOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"isMusicOn"];
    }
    else
    {
        self.isMusicOn = true;
    }

    if(self.isMusicOn)
    {
        [SimpleAudioEngine sharedEngine].backgroundMusicVolume = 1.0f;
    }
    else 
    {
        [SimpleAudioEngine sharedEngine].backgroundMusicVolume = 0.0f;
    }
}


-(void) applicationWillResignActiveUIApplication *)application
{
    [[NSUserDefaults standardUserDefaults] setBool:self.isMusicOn forKey:@"isMusicOn"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
@end


//Create Music button in your Cocos2d Scene

@interface MySettingsScreen : CCLayer 
{
        CCMenuItemSprite *mMusicBtn;
        . .  .
}

//in implementation file
-(void)createButton
{
    AppController *app = (AppController*)[UIApplication sharedApplication].delegate;

    NSString *imgName = ((!app.isMusicOn) ? @"mute.png" : @"musicOn.png" );

    CCSprite *music_1   = [CCSprite spriteWithSpriteFrameName:imgName];//spriteWithFile if png is from file
    CCSprite *music_2   = [CCSprite spriteWithSpriteFrameName:imgName];
    music_2.color = ccc3(200,200,200);

    mMusicBtn = [CCMenuItemSprite itemWithNormalSprite:music_1
                                             selectedSprite:music_2
                                                     target:self
                                                   selector:@selector(musicBtnPress ];

    mMusicBtn.position = ccp(mS.width*0.25f, mS.height*0.1549f);


}


-(void)musicBtnPressid)sender
{    
    AppController *app = (AppController*)[UIApplication sharedApplication].delegate;

    if(app.isMusicOn)
    {
        [SimpleAudioEngine sharedEngine].backgroundMusicVolume = 0.0f;

        app.isMusicOn = false;

        mMusicBtn.normalImage = [CCSprite spriteWithSpriteFrameName:@"mute.png"]; //spriteWithFile if png is from file
        mMusicBtn.selectedImage = [CCSprite spriteWithSpriteFrameName:@"mute.png"]; //spriteWithFile if png is from file

    }
    else
    {
        app.isMusicOn = true;

        [SimpleAudioEngine sharedEngine].backgroundMusicVolume = 1.0f;

        mMusicBtn.normalImage = [CCSprite spriteWithSpriteFrameName:@"musicOn.png"];
        mMusicBtn.selectedImage = [CCSprite spriteWithSpriteFrameName:@"musicOn.png"]; 

        [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"gr-theme.m4a" loop:YES];
    }

}

关于ios - 如何让音乐开/关设置objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20297580/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4