And Then It Rained - Animating with Enter Frame
In this video we start animating our particles and create a simple 'rain' where the elements go from up to down.
Video Transcript
00:00We want to listen to some sort of enter frame event, right?
00:05So let's do that little enter frame.
00:07And we're not going to attach an enter frame for every single oval
00:10because that would just be too crazy.
00:12It would be 100 enter frames happening at the same time.
00:14And that's exactly where you want to be smarter.
00:17And the way you could be smarter here,
00:18we could just do an enter frame for our direct route directly
00:22to our main and just cycle through our different movie
00:25clips in a loop every enter frame.
00:27So let's start with adding an event listener.
00:32Whoopsie.
00:34And we're going to add an event dot enter frame.
00:38And we already have imported the enter frame,
00:40so we don't have to worry about it.
00:42And we're going to call it on enter frame.
00:45All right, an on enter frame in the previous version of Flash
00:47and ActiveScript 2 was actually a recognized method that
00:54would already have the event.
00:55So it's turning blue, and we're going to get a warning message.
00:58But I don't really care because I just
00:59like the word on enter frame.
01:00It's not an invalid code.
01:02We'll get a warning, but we could ignore that warning.
01:04All right, so we're going to create another function.
01:07And that was an on enter frame.
01:09So don't worry about it turning blue.
01:11It's completely OK.
01:14And hopefully in one version of Flash, at some point,
01:17they'll just stop putting that error because there's
01:19nothing wrong with it.
01:21They were just trying to help from earlier adopters
01:24that are moving from ActionScript 2,
01:26not to think that it's already running by default on enter
01:29frame, which it's not.
01:31But we just love on enter frame.
01:32It's such a great name.
01:33Why call it anything else?
01:36Next step is now that we have our enter frame,
01:38we need to figure out a way how to loop through our elements.
01:40Now, if we wanted to, we could have just
01:42added every single MC oval into an array.
01:46And with that array, we could have run through that array.
01:49But let's look at a different option.
01:51Let's look at a different option.
01:52One of them, first of all, let's just
01:54ask because we know that there's only
01:56our ovals on our screen.
01:57So we don't have to worry about thinking about what
02:00if there are other types of elements on the stage.
02:03So we could just ask our current stage, our this.
02:07We could ask our this, how many children does it have?
02:10And the way we could ask that is by, well,
02:12we don't need a var.
02:13We could just ask, it's a property.
02:14It's a property that belongs to our main, which
02:16is extends a movie clip, which extends,
02:19as we saw in the list before, the display object container.
02:21Every display object container has this property,
02:24num children.
02:27And num children is going to return to us,
02:29how many children does that movie clip have?
02:31And we could see as we're running the enter frame,
02:33it's telling us it has 100 children.
02:35Great.
02:35So we know how many children it has.
02:37And we're keeping this dynamic, even though we
02:39know there are 100 because we added 100,
02:41just in case we change the number here
02:43with that we don't have to go back into our enter frame
02:45and redefine it again.
02:47So I'm just going to create a local variable.
02:48And I'm just going to call it num, which will be an int
02:53and will equal that value in num children.
02:56Let me just make our screen a little bit bigger.
02:59And now we could run through, again, a loop.
03:02And we could just run through.
03:04We could just grab that loop that we created before.
03:07And instead of using 100, we're just
03:09going to set the value of num directly into it.
03:13Now a smarter solution, and let's be smart.
03:16Why be less smart?
03:18We create a variable up on top before a constructor,
03:23something that's available for both our main
03:26and our enter frame.
03:27We could create a variable here, and we could call it children,
03:34or n children.
03:38Whoopsie.
03:38We said lowercase o is.
03:41n children, which will be an integer.
03:44And we're not going to give it a default value.
03:47But then, or why not?
03:49We're going to give it a default value of 100.
03:51And we could go into our 100 value, replace it,
03:55could comment that out.
03:57And then we could just put directly n children.
03:59So based on the amount of children
04:00that we're going to create, we're going to call it.
04:02So like that, if we want to change it from 100 really
04:05quickly to 50, we have no problem.
04:08And our logic here will continue working.
04:10We'll see that in a second.
04:11All right.
04:12So the next step is we want to actually fetch
04:14that element, or those random elements.
04:17To do that, we're going to approach, again,
04:19our visual display object.
04:21Now we don't know, we might not remember by heart,
04:24all these properties of the movie clip.
04:25And again, there's no shame in that, of course.
04:28And this might give us that information.
04:30And so it does.
04:32So we could type this just so we could get the references.
04:34And what we're looking for is to get our visual display,
04:40get child, by its index.
04:42Because we have a value index, which
04:43we know are numbers that go between 0 and whatever
04:47the value is.
04:47It's very similar to our arrays.
04:49So we want to get our child at position i.
04:53Now we don't need this.
04:54We just put it there just to help us to get some hints,
04:56because there's no hints without it.
04:59And now we could type dot and do whatever we wanted.
05:02So let's say we just want to move it always vertically,
05:04that we want to make our y one bigger always.
05:08Not very aesthetically smart, but let's see what would happen.
05:13So we could see that our elements are in the same speed.
05:16Doesn't matter if they're, in theory, further away or closer,
05:20are always going down in the same motion.
