• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何让音乐开/关设置objective-c

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:52:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap