如何让音乐开/关设置objective-c
Posted
技术标签:
【中文标题】如何让音乐开/关设置objective-c【英文标题】:How to have a music on/off setting objective-c 【发布时间】:2013-11-30 07:03:27 【问题描述】:我正在 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 selector:@selector(turnOffSound)];
sound.scale = 1.1;
menu2 = [CCMenu menuWithItems:sound, nil];
menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
[self addChild:menu2];
然后是我的turnOffSound
和turnOnSound
方法:
-(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 selector:@selector(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。你知道我怎样才能让它不再重置吗?感谢您的帮助,谢谢!
【问题讨论】:
尝试同步到 NSUserDefault。 【参考方案1】:试试这个,我使用了这个代码并且一直在工作。
@interface AppController : NSObject <UIApplicationDelegate>
bool isMusicOn;
…
@property (nonatomic, assign) bool isMusicOn;
@end
@implementation AppController
@synthesize isMusicOn;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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) applicationWillResignActive:(UIApplication *)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)musicBtnPress:(id)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];
【讨论】:
以上是关于如何让音乐开/关设置objective-c的主要内容,如果未能解决你的问题,请参考以下文章