Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Game with Sprite Kit Artificial Intelligence and Sound Adding Background Music

Shaun Kelly
Shaun Kelly
5,648 Points

I understand this video but I want to take this concept further with different background...

I have an if statement that picks out a random background. I have called this method within the didMoveToView. If the background first becomes either 0 or 1, the app crashes and i think it's because if the background is NOT the sea then it's trying to stop a soundtrack that hasn't been started yet... Any ideas on how to solve this ?

//PICKS A RANDOM BACKGROUND EACH TIME THE SCENE LOADS
-(SKSpriteNode*) pickRandomBackground {

   _randomBackground = [Utility randomNumberAt:0 max:3];
    //NSInteger randomBackground = 0;

    SKSpriteNode *background;





    //PYRIMID BACKGROUND
    if (_randomBackground == 0) {


         background = [SKSpriteNode spriteNodeWithImageNamed:@"background2"];
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)+20);
        background.size = CGSizeMake(self.frame.size.width, self.frame.size.height);



    }

    //FOREST BACKGROUND
    if (_randomBackground == 1) {

        background = [SKSpriteNode spriteNodeWithImageNamed:@"forestBackground"];
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        background.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
    }

    //SEA BACKGROUND
    if (_randomBackground == 2) {



        //SKY IMAGE
        background = [SKSpriteNode spriteNodeWithImageNamed:@"introbackground.png"];
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        background.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
        background.zPosition = -1;

        //FOREGROUND SEA
        SKSpriteNode *foregroundSea = [SKSpriteNode spriteNodeWithImageNamed:@"foregroundsea"];
        foregroundSea.zPosition = 1;
        foregroundSea.size = CGSizeMake(self.frame.size.width+200, self.frame.size.width/3.0);
        foregroundSea.position = CGPointMake(CGRectGetMidX(self.frame),70);
        foregroundSea.anchorPoint = CGPointMake(0.5, 0.0);
        [self addChild:foregroundSea];
        [foregroundSea runAction:[SKAction waitForDuration:0.7] completion:^{
            [foregroundSea runAction:[self waveAnimation]];
        }];

        //BACKGROUND SEA
        SKSpriteNode *backgroundSea = [SKSpriteNode spriteNodeWithImageNamed:@"backgroundSea"];
        backgroundSea.zPosition = 0;
        backgroundSea.size = CGSizeMake(self.frame.size.width+200, self.frame.size.width/4.0);
        backgroundSea.position = CGPointMake(CGRectGetMidX(self.frame),100);
        backgroundSea.anchorPoint = CGPointMake(0.5, 0.0);
        [self addChild:backgroundSea];
        [backgroundSea runAction:[self waveAnimation]];


           //WAVE SFX
           [self.seaSFX play];
    }

if (_randomBackground != 2) {
        [self.seaSFX stop];
    }


    background.zPosition = -1;
    [self addChild:background];
    return background;
}