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 Actions and Animations Extra Credit

Maarten Tolhuijs
Maarten Tolhuijs
13,349 Points

Alternative fade method

Hello all!

I created an alternative method that works for those occasions the projectile heads towards the top side of the screen instead of the left and right sides. Thought it couldn't hurt sharing it with all of you...

So here it is:

1: set the height to whatever screen it is you are using:

float heigth = self.parent.frame.size.height - self.position.y;   

2: create an if statement to see if the projectile is heading for the top or the side of the screen

float waitToFade;
    if (pointOffScreen.x >= 0 || pointOffScreen.x <= self.parent.frame.size.width) {
        waitToFade = heigth / THProjectileSpeed * 0.75;
    }

3: set the fadeTime with the new value of waitToFade (alternatively you could of course, or maybe even should, use the waitToFade variable as the fadeTime in "SKAction fadeOutWithDuration:fadeTime" in the NSArray *sequence)

float fadeTime = waitToFade;

From here you can use the same NSArray *sequence as the one being used in the example, so from the point where you started writing the fade method it should look something like this:

float heigth = self.parent.frame.size.height - self.position.y;
float waitToFade;
    if (pointOffScreen.x >= 0 || pointOffScreen.x <= self.parent.frame.size.width) {
        waitToFade = heigth / THProjectileSpeed * 0.75;
    }

float fadeTime = waitToFade;

SKAction *moveProjectile = [SKAction moveTo:pointOffScreen duration:time];
[self runAction:moveProjectile];

NSArray *sequence = @[[SKAction waitForDuration:waitToFade],
                      [SKAction fadeOutWithDuration:fadeTime],
                      [SKAction removeFromParent]];

[self runAction:[SKAction sequence:sequence]];

}

@end

2 Answers

Awesome! thanks for sharing this!