Adding 100 Visual Items With A Loop
Time for a loop. Its time we crack in the handly for loop and use it to add our visual element on the stage 100 times.
Video Transcript
00:00Now, the next step that we actually would want to do is actually go on to adding multiple
00:05items onto the stage and putting them in random positions.
00:11So let's take this little sample that we've just done right now, we're going to comment,
00:14we don't even have to comment it out to be honest.
00:17What we could do now is if we wanted to, let's say we want to add it 100 times, we
00:20could create a new for loop.
00:22We're going to tell this an I type and set it to zero as long as I smaller than 100
00:29and make I larger by one each time.
00:33Going to take this line, push it forward, whoops, these two lines, push them forward
00:41and not forget to close my bracket.
00:44Just by doing this alone, I'm going to create 100 instances, it's going to be
00:47impossible to notice because when I run it, what's going to happen, they're all
00:50going to be stacked one on top of each other.
00:53So let's just give them also some sort of randomization where they're in
00:57different positions of the stage when they're initially initialized.
01:00So let me also get rid of the word this because we don't need it, we just put it
01:03there for to explain what is actually going on.
01:09So let me also ask you a question, when I say add child, am I saying, is that a
01:14function or is that a method?
01:15And we talked about that a bit already before.
01:18So let me answer that question.
01:20Trace is a function, it's not an object.
01:23Sorry, trace is a function, it's not a method, why?
01:25Because trace doesn't belong to anything.
01:27Trace is a global function that is available to everything, but
01:30trace on its own has no scope.
01:33In other words, it doesn't really have a this, nothing is connected to it.
01:37Well, add child is not global, it's that you can't put it anywhere you want to.
01:41Add child is not a function only, it's a method.
01:44Why is it a method?
01:45Because add child belongs to the movie clip instance right now,
01:49it belongs to this main.
01:51Thus, if it belongs to this main, it's actually a method and
01:55it's an important differentiation cuz you can't just put add child anywhere you want to.
01:59You can put add child only to display object containers, right?
02:03That are on display object containers that you wanna add a child to them.
02:09Now adding a child doesn't always mean you're gonna see that child.
02:13To be able to see that child, you have to make sure that your parent is also on
02:16the stage and then you would see the child.
