Goal: I want to present a new scene:
[self.scene.view presentScene:level2 transition:reveal];
and end the current background music in order to start the new background music (the new background music from level 2).
TheProblem: Upon presenting the new scene the background music of the 1. scene (level1) keeps playing and DOES not stop even when leaving the miniGame since the whole game consists of few mini games.
The music played is an SKAction:
@implementation WBMAnimalMiniGameLvL1
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size])
{
/* Setup your scene here */
self.temporaryScore = 0;
self.animalSKSprites = [[WBMAnimalsMiniGameModel alloc] init];
self.backgroundImage = [SKSpriteNode spriteNodeWithImageNamed:@"farmBackground1024x768.png"];
self.backgroundImage.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
self.temporaryStartingPointIndex = -1;
[self addChild:self.backgroundImage];
self.playBackgroundSound = [SKAction playSoundFileNamed:@"farm.mp3" waitForCompletion:NO];
//SKAction *repeat = [SKAction repeatActionForever:playSound];
[self runAction:self.playBackgroundSound withKey:@"play"];
[self drawAllAnimalsOntoScreen];
}
return self;
}
Here does the transition to the next level happen:
-(void)transitionToNextLevel
{
NSLog(@"transitionToNextLevel");
SKTransition *reveal = [SKTransition moveInWithDirection:SKTransitionDirectionDown duration:0.5];
//SKView *skView = (SKView *)self.view;
SKScene *level2 = [[WBMAnimalMiniGameLvL2 alloc] initWithSize:self.size];
level2.scaleMode = SKSceneScaleModeAspectFill;
[self removeAllActions];
[self removeActionForKey:@"play"];
//self.scene.view.paused = YES;
//self.playBackgroundSound = nil;
[self.scene.view presentScene:level2 transition:reveal];
}
In the comments code lines you can se what I have already tried and what did not work. The :
[self removeAllActions];
[self removeActionForKey:@"play"];
did absolutely nothing. The:
self.scene.view.paused = YES;
line stops only the transition but the music still continues.
I have tried the following: - used a weak or strong property on a :
@property (nonatomic,weak) SKAction *playBackgroundSound;
to secure a pointer to the SKAction so I can access it beside using the "withKey" property since I initialized the SKAction in "initWithSize". Someone wrote that SKAction is a fire-and-forget object which I understood as without having stored a pointer to it accessing it later is not possible (directly). However, it didnt work/help me.
I looked into many other stackoverflow posts and none helped me or at least gave me a clue why this happens:
SKAction playSoundFileNamed stops background music
Stop SKAction that RepeatsForever - Sprite Kit
Is it possible to end an SKAction mid-action?
Pausing a sprite kit scene
... Thoughts: It seems that the action is created with the creation of the SKScene object. It is "hooked" to it and finished after the audio duration. While I was using repeatForever it would never stop. However there is no way to pause or stop it. I also tried hooking the SKAction to SKSpriteNode. When the SKScene is loaded animals are loaded with it. So I tried hooking the SKAction on the SKSpriteNode and use removeAllActions and alike from the SKSpriteNode but it didnt work aswell.
I checked the documentation on SKAction,SKView,SKScene,SKSpriteNode but in the end it didnt help me much.
It looks like the object exists somewhere running its actions.
What the error is not about:
It is not simulator or device related error - I have tested it on both the simulator and device with the same result (error).
It is not a project related error - I have tested it in a separate project , much smaller and the with the same result (error).
A temporary solution: I have been using the AVAudioPlayer class from AVFoundation framework. I created a :
//@property (nonatomic) AVAudioPlayer *theBackgroundSong;
This didnt help much since I wanted to change theBackgroundSong when level2 was loaded and I had huge problems accessing the property from a nested SKScene structure.
Any advice,clue,hint or idea would be of great help.
When you apply a compressor or adjust the tracks to get a certain level of loudness, some conscious clipping can occur. In this context, clipping can be used to reduce the peaks of a song in an often inaudible way. However, of course, if you’re worried about your audio clipping, chances are there’s nothing intentional about it.
Audio clipping can damage the tweeters in your sound speakers if the volume is too high or the clipping happens for an extended period. To avoid this, many speakers have built-in circuits that act as soft limiters. They prevent the system from clipping, but the audio quality will still suffer. What’s happening is a kind of audio distortion.
Type Control Panel and press Enter. 3. Click on Hardware and Sound > Devices and Printers. 4. Double click on the controller and go to Hardware tab. 5. Click on the Controller with a speaker icon. 6. Click on Properties and disable it. 7. Unplug and plug it back and check. Was this reply helpful? Sorry this didn't help. Great!
Use a pen or a needle and push the button for 10 seconds. 4. Connect the controller and check. If the issue persists, follow method 2. Method 2: Disable the audio and check. 1. Click on Start ico n. 2. Type Control Panel and press Enter. 3. Click on Hardware and Sound > Devices and Printers.
Now you can stop playing sound action by assigning key string for it then using removeActionForKey
later
Sorry, this is Swift. But it can be ported to Obj-C.
let action: SKAction = SKAction.playSoundFileNamed("sounds/boom.wav", waitForCompletion: true)
self.runAction(action, withKey:"die-sound")
// later you do:
self.removeActionForKey("die-sound")
I have tested successful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With