メイン メニュー (「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
}
}