This course will be retired on January 24, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's give the illusion that the projectile is moving away from the user by fading it out. Fade out the projectile using a sequence of SKActions when it reaches three-quarter of the distance from its original position. (hint: you would need to add this functionality to the moveTowardsPosition method).
Hints
Use the following methods within SKAction to create the fade effect:
- fadeOutWIthDuartion
- waitForDuration
- sequence
Documentation
-
0:00
All right.
-
0:00
It's time for extra credit once again.
-
0:02
So, this time, as the description says, what we want you to do is we want you to
-
0:07
fade out the projectile once it reaches 75% of the distance traveled.
-
0:12
So, let me show this to you again.
-
0:16
There, it just faded out in.
-
0:18
Because without the fade out effect, the projectile just stays
-
0:23
the way it is throughout the distance that it's traveling.
-
0:26
And I mean, it's a subtler effect, but it kind of gives you the effect
-
0:30
that it's going away and it's traveling a
-
0:33
distance and it's moving away from the user.
-
0:36
So, I'll be right here, take a crack at it.
-
0:39
Just to show you how you can implement it, I'm
-
0:41
going to open up the documentation and look at SKAction.
-
0:45
I'm gonna give you a few hints here.
-
0:48
So firstly, you might want to use a couple of the things.
-
0:52
So, there's a wait action.
-
0:56
So, you have a Wait For Duration action, and then you have
-
0:59
another action called Fadeout, so that's how you can fade it out.
-
1:04
And then, you can group actions by using a sequence, so what you can
-
1:09
do is you can define an array of several different actions and then create
-
1:13
yet another action that says, hey, this is a sequence of actions that I
-
1:17
want you to execute in a linear fashion, and then just run that action.
-
1:24
So, those are your hints, you know, use a sequence of
-
1:27
actions then define these several different actions and make this happen.
-
1:31
I'll be right here waiting.
-
1:33
So, go ahead and pause or stop this video, and take a crack at it, and
-
1:38
when you come back, I will give you the solution on how to make this happen.
-
1:43
All right.
-
1:44
Welcome back.
-
1:45
I hope you took the time to actually try to see if you can make this happen.
-
1:49
So firstly, I have to say that I had to decrease the
-
1:52
projectile speed so that I could demonstrate the fade out feature to you.
-
1:57
I'm going to go back and set that to 400.
-
2:00
And then going over to our projectile node implementation file.
-
2:05
So, here at line 65 after we run the action to
-
2:09
move the projectile, what I did was I created two more variables.
-
2:14
One is waitToFade and waitToFade basically takes time and
-
2:19
multiplies that by 0.75 which is 75% of the time.
-
2:24
And then the fadeTime is basically, time minus waitToFade.
-
2:30
Then, I declare three actions, the first action is wait
-
2:34
for duration and I pass it the wait to fade.
-
2:37
The next action is the fade out with duration and this the fade time I give it.
-
2:43
So, this is the amount of time it will be fading the projectile.
-
2:47
And finally, I just give it the action to remove from parent.
-
2:51
Now, generally the scene manages this for us, but it's always
-
2:55
good practice to remove notes that are no longer being used.
-
2:59
So that's why I just added that for good
-
3:01
measure, because if you increase the complexity of your game,
-
3:04
then it kind of becomes hard for the scene to
-
3:07
track which notes are being used and not being used.
-
3:09
So, it's always good to follow best practices to remove notes, even though
-
3:14
those nodes will eventually be removed by
-
3:16
the scene, especially if they're off screen.
-
3:19
And then finally I create an action called SKAction sequence, and
-
3:24
I give it my array of actions and I run that action.
-
3:27
So, that's pretty much it you know, you
-
3:30
should create variables to wait for durations and
-
3:33
fade out and then remove from parent, and then you just run the sequence of actions.
-
3:38
Now, I'm sure your wheels are turning and you can
-
3:41
think of multiple different, little techniques that you can do.
-
3:45
So, I hope this inspires you to
-
3:47
create some creative little actions with projectiles or
-
3:50
with a cat or with, you know, any other objects that we're going to add.
-
3:56
So, now in our next stage, we'll take this a step further, and we'll
-
3:59
start adding our space dogs, our enemies and see how we can interact with that.
You need to sign up for Treehouse in order to download course files.
Sign up