02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Animating a Fire

Now that we have our art on stage and its ready to animate its time for us to start animating. More or less at this stage we know what we want but we don't know exactly what we want. In this video we start exploring optional animations until we feel its right. We start with animating one of the layers and then learn a few more tricks regarding reusing of code and how we can reuse our animation for other layers. By the end of this video we will have a working fire. We will create a stop method that will enable us to stop our animations and start them. This is in a goal to enable us to control the animations from our main class. Notice that even though we don't actually call the touch method it will automatically trigger when user clicks on the fire when published! Nifty right? This is happening because in an earlier class we set a mouse event that calls the touch in the main - Even before we created the class everything was set in place! Really cool and exactly the point of polymorphism and interfaces. The idea of not needing to worry about totally separate blocks of code but instead building out the internal logic set knowing the external one will work as it was worked on earlier and we do not need to change anything within it.

Video Transcript

00:00Oh, as always, I see a little typo here,
00:02so let's fix it to MCFire.
00:05It's not a fire, or we'll get an error, which is fine,
00:09but if we could avoid it, let's avoid it.
00:13All right, so we have it, we'll get an error action,
00:15we start working with it,
00:16because it's totally fine to define a variable
00:18that's empty, but in our case, we do have a fire,
00:21and our fire is called MCFire,
00:23and we want to coordinate with the names,
00:24MCFire1 and MCFire2.
00:27All right, so we have our MCFires.
00:29The next stage that we want to do is we want to make sure
00:31that we give it some sort of a random alpha value,
00:35and lucky for us, we could use the math random function,
00:38and we'll do that also for fire too.
00:44Fantastic, if we run it, we'll see that our fire
00:46will have now this more fiery feel to it.
00:49Really, all that's left for us to do is to start
00:51maybe kind of like playing around with the animation a bit.
00:56It's really not going to be so, you know,
00:58completely realistic, but we're just going to get it
01:00as fun as we can without going too crazy with it,
01:03and really, all we're going to do is going to fade in
01:05and out a bit the fire to give a sensation of movement
01:09and that something is going on.
01:10So we want to do in our touch function,
01:12we want to make sure that we call add event listener.
01:16We're going to add a new enter frame event,
01:18which is part of the regular events, a movie clip,
01:23and we're going to call on enter,
01:24which will be our function name.
01:26We'll create a private function.
01:28Let me just open this up.
01:30Open this up so it's easier to see.
01:32Public function on enter, which is an event.
01:42And on enter, we do want to play with the values.
01:45Now we do have, we don't really want it to go
01:47from zero to one and from one to zero.
01:50We really want them always to kind of like
01:52stay in that 50 to the 100%.
01:55Now what we could do is we could animate
01:57the first one towards 50 and the other one to 100%
02:03and vice versa and kind of flip between the two
02:05more or less in the same speeds,
02:07see how that works out and then define something else
02:10if it doesn't.
02:12So we want to create here some sort of something
02:14that will track our direction at any given time.
02:19Let's do it.
02:20All right, so first of all,
02:21we're going to create a public variable.
02:23Oh, it actually could be private.
02:25So private variable, private bar.
02:30What am I doing?
02:31So it's going to be, we're going to use a number
02:33and that number is going to be just called direction one,
02:36direction two, in connection to our fire.
02:38So direction one, we're not going to start with dir
02:41just because ndir, one.
02:45The only reason I'm not doing that
02:46is because there's a directory
02:48and it might be confusing for someone
02:49that doesn't know our code.
02:50ndir at least will be closer to the idea of a number.
02:55Really our values that could be inside here
02:57are only one or minus one.
03:02Um, two, which is a number,
03:06and we're going to set that to be one as well.
03:08But really, the truth is is that we probably want to check
03:11the value in our MC fires and basically reset our value
03:15as soon as after we already set their value.
03:18So if our alpha level is greater than 0.5,
03:23we'll, let's not even do it randomly.
03:28You know what, let's just keep it simple in the beginning
03:30and then we can always tweak it
03:31and add some randomness if we want.
03:33So let's have the first one at 0.5 and the MC fire at one
03:37and just have them kind of like, as soon as we start,
03:40one is at 0.5, one is at one,
03:42this one will go up, this one will go down.
03:45And then we'll flip the two around.
03:48All right, so in our answer frame,
03:50let's start with just animating one of the two.
03:51We'll animate first MC fire one
03:53and only after that we'll kind of figure out
03:56how do we want to actually really deal with it.
03:58All right, so MC, what we're going to do first of all,
04:01is we're going to first of all check to see
04:04if our new value that will come back
04:06is going to flip the direction or not.
04:08We'll first set up that value.
04:10So we'll call it step size one, which is going to be a number.
04:16And that value, we're just going to take our n dir one
04:20and we're going to duplicate it by,
04:22let's just give it as a value of 002.
04:25And we could always make it bigger or smaller.
04:28And really what we're going to do is MC fire one
04:31dot alpha, alpha plus equals step size one.
04:39Now, the next step that we want to do
04:41is we want to actually kind of like confirm its direction.
04:44If the direction, if it's time for the direction to change,
04:46then we want to update and change it.
04:47So if our MC fire value alpha is greater than one,
04:53then greater or equal to one, we want to do one thing.
04:58Else, if our MC fire is smaller or greater than 0.5,
05:09then we want to do something else.
05:11All right, so if our value is greater or equal to one,
05:15then what we want to do is we want to set our direction
05:18to go down, so our n dir one will be equal minus one.
05:22On the other hand, if our value is smaller than 0.5,
05:26then we want our value to change up to one.
05:28And really, it doesn't really matter
05:29what value we started off with.
05:31Whenever we get that, we shouldn't be getting the cycle
05:34and going back and forth.
05:34And let's see if that's really true.
05:36And I'm just going to add here another trace out
05:38just from MC fire one dot alpha
05:41to kind of track and see our direction
05:44and our numbers as they're changing.
05:45So if we run it and let's just fix this up,
05:48we have here a little typo.
05:50So it should be an uppercase D.
05:52So we'll fix that.
05:54Oops, uppercase D, uppercase D.
05:57Run this maybe one more time.
05:59So first of all, we're stuck at 0.5,
06:01meaning that we're not getting anywhere.
06:03Let's see why that's actually happening.
06:05If we trace out our step size one,
06:09let's see what value that is
06:10to see if there's anything there
06:12to help us figure out what's going on.
06:16And we could see that our value is kind of like 0.02.
06:23Oh yeah, that makes sense,
06:24because we're, yeah, okay.
06:25So the big question is, is why isn't our value changing?
06:29And it could be because this is just too small
06:33for our actual alpha to actually care about.
06:35It probably could only go down in one at a time.
06:38It can't go less than that.
06:40So if we run this run one more time,
06:43now we'll see that the values are changing.
06:46They're very slow, not that interesting, right?
06:49So how about we change it up in a way
06:52that might make it more visible?
06:54So let's change it to maybe four.
06:59And now we could definitely see that there's a fiery
07:01kind of a glimmer going on,
07:02and I actually really like this.
07:04So we're gonna keep that,
07:05and we're gonna add in Oto our second one.
07:08And if we're already doing this, a good thing to do,
07:10we could show you something really, really cool.
07:12Now, our animation and all this code is exactly,
07:16should be exactly the same thing for this
07:17and also for step size two.
07:19Our only difference really is,
07:21is between what are we duplicating.
07:23And because of that,
07:25what we could do is let's,
07:26step size, let's just get rid of that one.
07:29We don't need it if there was only one in our function.
07:33We can get rid of the MCFire one,
07:34but what we can do,
07:36if we create a new private function,
07:39and we could call this update fire,
07:42and it will take in an MC fire,
07:45or it will take in a fire, which is a movie clip.
07:49It will not return anything.
07:51If we take all this and we kind of like move up
07:53and we move all of this right into our update fire,
07:58we could delete this trace.
08:00And let's try to make it more anonymous
08:01and see where do we get stuck
08:04in our anonymization process.
08:07All right, so we changed everything that we could.
08:09Now we have only one thing left,
08:10which is the end dir one,
08:12which obviously is problematic for us to be able to bypass.
08:16Now, we can't really send it as a parameter
08:19because if we send it as a parameter,
08:21then it'll be kind of hard for us to keep it.
08:25Now, what we could do, it'll be hard for us to update it.
08:27We would have to send it back and we'd have to update it.
08:29It would be really great if we had a direct way
08:31to tap into our end dir one and dir two,
08:34and there actually is.
08:36So what we could do is we could create here
08:38some sort of maybe a parameter
08:39that enables us to actually trigger
08:42the specific one that we want,
08:44or even more so we could probably read out directly
08:47from the movie clip.
08:49And probably the best solution would be
08:50to just put the variable into the movie clip
08:52because it's a movie clip.
08:53But we're not going to do that.
08:54We're going to take the long path,
08:55and the long path is going to be here,
08:59layer num, which will be for the layer number.
09:03It's going to be an integer.
09:04The values are going to be one or two,
09:06but if we had more layers,
09:07we could put there more than that.
09:08And what we could do is we could create a new variable
09:11and call it end dir, which is going to be a number.
09:16Actually, what is end dir?
09:17It should be an integer.
09:18It doesn't need to be a number
09:20because it will be only full values.
09:22It would never be partials.
09:24So our end dir could be an integer.
09:28Oh, yeah, no, that's fine.
09:30All right, and now we could set into it.
09:31We want to call really this end dir.
09:33So what we could do is we call this dot,
09:36well, open a bracket,
09:39and we could define that exact name
09:42and kind of like approach it directly
09:44by concatenating the values.
09:46So basically, I'm going to this,
09:47and instead of using the dot syntax,
09:49we kind of covered it in the developer basics,
09:51but in case you don't remember it, that's totally fine.
09:53And we could approach directly our variable internally,
09:56and because it's there,
09:57we shouldn't have any issues inside of Flash.
09:59Some compilers will not be happy with this,
10:01but our Flash CS's compilers are totally fine with it.
10:04And it's a really great way of fetching our information.
10:08So I'm going to grab my end dir,
10:09and now I got my variable,
10:11and it could be any number
10:12depending on the number that came in.
10:13And I can now change the end dir references
10:16to end dir one to just end dir.
10:18And now, really, I didn't change anything in my logic,
10:21and you could see how you could create
10:22these progressive updates to your code
10:25without needing to over plan.
10:26So you've built something, and it works.
10:28You could always continue.
10:30So I'm going to try and trigger it
10:31and actually see that it works.
10:32We're going to start with mcfire one
10:34and ask to update our fire.
10:36And if I run this one time,
10:38we'll see that our fire is working?
10:44Well, not really.
10:47Let's give it one more shot
10:49before we start debugging it.
10:51So first of all,
10:51I'm not seeing any real animation going on.
10:53So what I'm going to do is I'm going to trace out
10:55to see if I'm getting all the information
10:57that I need to get.
10:58Oh, well, first of all, no, everything here is fine.
11:01So I'm going to end dir.
11:03I'm going to trace our end dir
11:04to see that we're getting the value back.
11:06And we definitely are getting the value back.
11:08Now, I already know what my problem is,
11:10and my problem is that I'm setting this variable,
11:13this new variable,
11:14and this new variable is in redefined in line 34 and 36.
11:19This is a fantastic bug
11:20and a fantastic mistake that I made right now,
11:23because it exactly goes back
11:24to one of our developer basic courses
11:26that said that a primitive value
11:28when we copied into a new variable,
11:30it's a completely new variable.
11:32And thus in line 35 and 36,
11:34when I'm flipping the direction,
11:36I'm flipping the direction to a completely new variable,
11:38which is end dir.
11:39So if I really wanted to change the value,
11:41I would have to reference this value directly each time,
11:45making my code kind of ugly,
11:47but making it work, all right?
11:50So if I run it again,
11:51we should hopefully see that our animation now is back up
11:54and it truly is,
11:55and now our animation is working.
11:57So all that's left for us to do
11:58is actually to call mcfire2 as well.
12:01And obviously if we had more layers here,
12:03this could have been really, really interesting.
12:06And this is going to be our fire,
12:08and it looks actually really, really awesome.
12:10So all that's left for us to do, in my opinion is,
12:14is to not automatically call our touch,
12:16let our program itself to figure out that touch thing.
12:20And then we probably want to add some sort of a interval
12:24and maybe a timeout.
12:26So we're going to create an import flash,
12:30set utils,
12:33set timeout,
12:35import import flash utils,
12:42clear timeout.
12:43All right, so first of all,
12:44let's create a new private variable,
12:50interval, which is going to be an integer,
12:56and we're going to set a timeout in our touch.
12:59But as always, as I love doing,
13:01before I do anything,
13:02I clear an interval or a clear timeout in our case,
13:07and we want to set a new timeout.
13:18And our, oop, I'm not typing this right.
13:23Set timeout, oh, lowercase, set timeout.
13:26All right, so set timeout,
13:27what we want to do is we want to call the function,
13:30we want to call the function,
13:31let's call that function stop.
13:34And we're going to call stop
13:35and the delay will be of two seconds,
13:37so 2000 milliseconds.
13:41Let's create that stop function.
13:43But first of all, we want to,
13:46yeah, yeah, let's just create that stop function.
13:48So our stop function is going to be public,
13:51and I'm going to explain why in the next video.
13:55Stop, and our stop function is going to be very, very simple.
13:59We're going to clear our interval,
14:01or our timeout will be over now, really,
14:04because we only set a timeout.
14:05But what we want to do
14:06is we want to get rid of our enter frame.
14:08Now, if you haven't noticed,
14:09I'm going to, instead of add event listener,
14:11I'm going to remove event listener,
14:12just copying our event.
14:14So basically what I'm doing is as soon as two seconds pass,
14:16my interval should terminate.
14:18Now, our touch should automatically happen
14:21because we're in itouchable,
14:22so as soon as we click on it, it should start animating.
14:25So let's see if that works
14:26before I give any other further comments
14:28so we don't get out of the loop.
14:29So our cup, dog, and cat are still clickable.
14:33Let's see if our fire is.
14:38Oh, there we go.
14:39So our fire is glimmering.
14:40Not very noticeable on a video, but it is glimmering,
14:43which is kind of cool.
14:44It doesn't need to be too noticeable.
14:45Download the source file is actually really cool.
14:47It looks really cool.
14:48All right, so it's definitely working.
14:50It does stop as well.
14:52Now, what I wanted to say before we wrap this video up
14:56is that literally what we're doing here,
14:59and this is, okay, this is the line
15:00that I really wanted to talk about, which is line 22.
15:03In line 22, we're setting an enter frame,
15:05but what happens when we're actually going to click
15:07on this fire a couple of times?
15:08If we click on this fire a couple of times,
15:10it shouldn't change too much.
15:13And that's, by the way, this is a great way to check
15:16if a code is broken or not or has issues,
15:18is if you click on something a lot of times,
15:20if it breaks it.
15:22Now, when we're clicking this
15:24and we're clicking it a lot of times,
15:25you'll see that our fire will still run
15:27and it's not going to run more times.
15:29It's going to run the exact same amount of time,
15:31no matter how many times we click on it.
15:33Now, the reason behind it is very simple.
15:34And by the way, the more times we click in it,
15:36all we're doing is resetting that time out.
15:39Now, the reason is when you add an event
15:41to a movie clip and you add the exact same event,
15:44Flash is only going to treat it once.
15:46It's not going to add it over and over and over.
15:48It's only going to add it one time.
15:49So we don't need to add your extra logic
15:51to check to see if we added the interval already.
15:54All we have to do is add it.
15:55After we added it, everything else is totally fine.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!