ios - 如何让音乐开/关设置objective-c
<p><p>我正在 cocos2d 中制作一个简单的游戏,我想制作一个简单的按钮切换来保持声音的开启或关闭。这就是我到目前为止所做的......</p>
<p>在我的<code>.h</code>中:</p>
<pre><code>CCMenuItemImage *sound;
CCMenu *menu2;
</code></pre>
<p>在我的<code>init</code>中:</p>
<pre><code>[ setBool:TRUE forKey:@"musicon"];
// Sound Buttons
sound = ;
sound.scale = 1.1;
menu2 = ;
menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
;
</code></pre>
<p>然后是我的 <code>turnOffSound</code> 和 <code>turnOnSound</code> 方法:</p>
<pre><code>-(void) turnOnSound {
[ setBool:TRUE forKey:@"musicon"];
;
;
sound = ;
sound.scale = 1.1;
menu2 = ;
menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
;
}
-(void) turnOffSound {
[ setBool:FALSE forKey:@"musicon"];
;
;
sound = ;
sound.scale = 1.1;
menu2 = ;
menu2.position = ccp(screenCenter.x - 33,screenCenter.y / 5);
;
}
</code></pre>
<p>现在,切换只在这个屏幕(这是标题屏幕)上有效,因为当我转到另一个屏幕,然后返回标题屏幕时,BOOL 再次设置为 true。你知道我怎样才能让它不再重置吗?感谢您的帮助,谢谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个,我使用了这个代码并且一直在工作。</p>
<pre><code>@interface AppController : NSObject <UIApplicationDelegate>
{
bool isMusicOn;
…
}
@property (nonatomic, assign) bool isMusicOn;
@end
@implementation AppController
@synthesize isMusicOn;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([ objectForKey:@"isMusicOn"] != nil)
{
self.isMusicOn = [ boolForKey:@"isMusicOn"];
}
else
{
self.isMusicOn = true;
}
if(self.isMusicOn)
{
.backgroundMusicVolume = 1.0f;
}
else
{
.backgroundMusicVolume = 0.0f;
}
}
-(void) applicationWillResignActive:(UIApplication *)application
{
[ setBool:self.isMusicOn forKey:@"isMusicOn"];
[ synchronize];
}
@end
//Create Music button in your Cocos2d Scene
@interface MySettingsScreen : CCLayer
{
CCMenuItemSprite *mMusicBtn;
. ..
}
//in implementation file
-(void)createButton
{
AppController *app = (AppController*).delegate;
NSString *imgName = ((!app.isMusicOn) ? @"mute.png" : @"musicOn.png" );
CCSprite *music_1 = ;//spriteWithFile if png is from file
CCSprite *music_2 = ;
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*).delegate;
if(app.isMusicOn)
{
.backgroundMusicVolume = 0.0f;
app.isMusicOn = false;
mMusicBtn.normalImage = ; //spriteWithFile if png is from file
mMusicBtn.selectedImage = ; //spriteWithFile if png is from file
}
else
{
app.isMusicOn = true;
.backgroundMusicVolume = 1.0f;
mMusicBtn.normalImage = ;
mMusicBtn.selectedImage = ;
[ playBackgroundMusic:@"gr-theme.m4a" loop:YES];
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何让音乐开/关设置objective-c,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20297580/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20297580/
</a>
</p>
页:
[1]