ios - 停止 SKAction
<p><p>我有一个包含不同场景的 Sprite 套件游戏:主菜单(“MainMenuScene”)和游戏场景(“MyScene”)。当用户在玩游戏时,我有一个没完没了的播放背景音乐。但是当玩家想要停止游戏并返回主菜单时,背景音乐一直在播放。我应该怎么做才能让它停止?我试过 <code></code> 但没用。</p>
<p>我的场景:</p>
<pre><code> @implementation MyScene
{
SKAction *_backgroundMusic;
}
-(id)initWithSize:(CGSize)size {
if (self = ) {
self.backgroundColor = ;
}
//Here I make the endless background music
_backgroundMusic = ;
SKAction * backgroundMusicRepeat = ;
;
return self;
}
- (void)selectNodeForTouch:(CGPoint)touchLocation
{
SKSpriteNode *touchedNode = (SKSpriteNode *);
if () {
SKScene *mainMenuScene = [initWithSize:self.size];
;
//Here is where the music should stop, when the player presses the 'return to main menu' button
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我不建议使用 SKAction 播放背景音乐。而是使用 AVAudioPlayer。</p>
<p>使用 AVAudioPlayer:</p>
<ol>
<li><p>将 AVFoundation 添加到您的项目中。</p></li>
<li><p><code>#import <AVFoundation/AVFoundation.h></code> 到您的 .m 文件中。</p></li>
<li><p>在@implementation中添加<code>AVAudioPlayer *_backgroundMusicPlayer;</code></p></li>
</ol>
<p>使用此代码段运行您的音频:</p>
<pre><code>- (void)playBackgroundMusic:(NSString *)filename
{
NSError *error;
NSURL *backgroundMusicURL = [ URLForResource:filename withExtension:nil];
_backgroundMusicPlayer = [ initWithContentsOfURL:backgroundMusicURL error:&error];
_backgroundMusicPlayer.numberOfLoops = -1;
_backgroundMusicPlayer.volume = 0.8;
_backgroundMusicPlayer.delegate = self;
;
;
}
</code></pre>
<p>还可以阅读 <a href="https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Chapters/Reference.html" rel="noreferrer noopener nofollow">AVAudioPlayer Class Reference</a>这样您就知道所有属性的作用,例如设置音量、循环次数等...</p></p>
<p style="font-size: 20px;">关于ios - 停止 SKAction,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23640296/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23640296/
</a>
</p>
页:
[1]