<分区>
在 gameScene.m 中,我们如何在玩游戏时使用 skspritenode 制作开/关音乐,使用下面的代码我可以完美地播放声音但是我想在用户想要打开或关闭游戏中的声音时打开/关闭玩自己的 ipad。
//.h file
#import <SpriteKit/SpriteKit.h>
@interface MyScene : SKScene
//.m file
#import "MyScene.h"
#import <AVFoundation/AVAudioPlayer.h>
@interface MyScene() <SKPhysicsContactDelegate>
@end
@import AVFoundation;
@implementation MyScene
{AVAudioPlayer *_AudioPlayer;}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_gameLayer.speed > 0) {
//for flying plane
_playerPlane.physicsBody.velocity = CGVectorMake(0, 0);
[_playerPlane.physicsBody applyImpulse:CGVectorMake(0, 14)];
[self jumpsound];
}
-(void)didBeginContact:(SKPhysicsContact *)contact{
_gameLayer.speed = 0;
[self removeAllActions];
skspritenodeGameOver.hidden = NO;
[self hitSound];}
- (void)jumpsound
{
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"jump" ofType:@"wav"]];
_AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[_AudioPlayer setVolume:0.5];
[_AudioPlayer play];
}
- (void)hitsound
{
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"jump" ofType:@"wav"]];
_AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[_AudioPlayer setVolume:0.5];
[_AudioPlayer play];
}
@end