【问题标题】:Stopping an SKAction停止 SKAction
【发布时间】:2014-07-01 15:51:57
【问题描述】:

我有一个包含不同场景的精灵套件游戏:主菜单(“MainMenuScene”)和游戏场景(“MyScene”)。当用户在玩游戏时,我有一个没完没了的播放背景音乐。但是当玩家想要停止游戏并返回主菜单时,背景音乐一直在播放。我应该怎么做才能让它停止?我试过[self removeAllActions],但没用。

我的场景:

    @implementation MyScene
{
    SKAction *_backgroundMusic;
}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.5 blue:0.3 alpha:1.0];
    }
    //Here I make the endless background music 
_backgroundMusic = [SKAction playSoundFileNamed:@"Background 2.m4a" waitForCompletion:YES];
    SKAction * backgroundMusicRepeat = [SKAction repeatActionForever:_backgroundMusic];
    [self runAction:backgroundMusicRepeat];
    return self;
}

- (void)selectNodeForTouch:(CGPoint)touchLocation
{
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
    if ([_MainMenuButton isEqual:touchedNode]) {
        SKScene *mainMenuScene = [[MainMenuScene alloc]initWithSize:self.size];
        [self.view presentScene:mainMenuScene];
//Here is where the music should stop, when the player presses the 'return to main menu' button
    } 
}

【问题讨论】:

  • 你真的不应该在这里使用 ivars 而是一个强大的属性。您能否在删除之前检查该操作是否不为零。我想可能是零。只是检查一下哈哈!
  • 无论你做什么,SKAction发起的音频都不会结束。
  • 我的意思是,不是立即...

标签: ios objective-c sprite-kit background-music skaction


【解决方案1】:

我不建议使用 SKAction 播放背景音乐。而是使用 AVAudioPlayer。

使用 AVAudioPlayer:

  1. 将 AVFoundation 添加到您的项目中。

  2. #import <AVFoundation/AVFoundation.h> 到您的 .m 文件中。

  3. 在@implementation 中添加AVAudioPlayer *_backgroundMusicPlayer;

使用此代码 sn-p 运行您的音频:

- (void)playBackgroundMusic:(NSString *)filename
{
    NSError *error;
    NSURL *backgroundMusicURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
    _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    _backgroundMusicPlayer.numberOfLoops = -1;
    _backgroundMusicPlayer.volume = 0.8;
    _backgroundMusicPlayer.delegate = self;
    [_backgroundMusicPlayer prepareToPlay];
    [_backgroundMusicPlayer play];
}

还请阅读AVAudioPlayer Class Reference,以便了解所有属性的作用,例如设置音量、循环次数等...

【讨论】:

    【解决方案2】:

    试试这个来播放音乐:

      [self runAction:backgroundMusicRepeat withKey:@"bgmusic"];
    

    然后停止:

      [self removeActionForKey:@"bgmusic"];
    

    更新

    SKAction * backgroundMusicRepeat = [SKAction playSoundFileNamed:@"Background 2.m4a" waitForCompletion:YES];
    backgroundMusicRepeat = [SKAction repeatActionForever:backgroundMusicRepeat];
    
     [self runAction:backgroundMusicRepeat];
    

    我已经在我自己的项目中运行了这些代码,并且看起来很有效。 但不是你的方式,它只会在我退出视图时停止,我什至不需要 removeActionForKey[self removeActionForKey:@"bgmusic"]; 在场景中不起作用。

    所以如果你想在同一视图的不同场景之间切换时停止声音,我建议你使用 AVAudioPlayer。

    我还发现stackoverflow中的一些其他问题和你有同样的问题,像这样: how to pause the sound when use SpriteKit 和这个: Spritekit stopping sound 他们都选择了 AVAudioPlayer。

    正如这些链接中的一条评论所说:你应该使用 playSoundFileNamed 方法来播放声音效果...短 1 或 2 秒的东西,比如爆炸声 --不要将它用于背景声音。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-29
      • 2015-02-24
      • 2014-06-05
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2017-05-20
      • 2023-03-03
      相关资源
      最近更新 更多