Loop To Repeat An Action
In this sample we learn how to use loops to repeat an action as we start thinking about how can we use a loop even if our content isn't exactly the same.
we take a peak at the relationship between the for loop and the Array with some tips on how to work with for loops in flash
Video Transcript
00:00Loops are a very powerful thing and they have many usages and in many formal
00:06documentations if you read online or you download or read a book or watch any
00:12video from other sources you'll see that most of the time when people talk
00:15about loops they talk about repeating things over and over and although that's
00:19true and in the heart of it a loop basically enables you to repeat an
00:23action over and over. Loops have a lot of hidden really powerful features and
00:29with them a lot of risks. As we saw in earlier video that loops we could create
00:35endless loops and as we go into the world of loops we want to make sure that we
00:39really use loops only when we need them because overusing loops will actually
00:43make our code less robust and although it's going to cut down the amount of
00:47code that we need to type it's going to add exponentially a lot more
00:52heaviness or a lot more load our application because our application is
00:56going to need to go over many commands many more times than what it would
01:00normally when you would type things straight ahead. With that said obviously
01:05loops are really critical and important foundation blocks in any programmers
01:10life. Now before we go into that world and try to see how do we leverage loops
01:15and when do we use them and when don't we use them we first really need to
01:19understand their core usages and there are probably two main places where you're
01:24going to use loops in a normal build in a normal development cycle. The first one
01:31is going to be the one we're going to look into in this video which is when you
01:34want to repeat some sort of action and we're going to go through some really
01:40simple action in this scenario but we've seen already an action that has been
01:44repeated such as duplicating on the stage multiple movie clips. The second type
01:50of usage for for loops is to actually do some math or to figure out something or
01:56search for something. So loops can be used as just straight ahead you know
02:02copy paste kind of actions of repeat with some various modifications to make it
02:07look dynamic as we've seen that we used a function and added some randomness
02:12into it. Or we could use our for loops to search for things or do some
02:19mathematical operations. So let's start with our first usage as we said is
02:24duplicating and at the same time we could repeat the structure of our for loop.
02:27So as we said we would start our for loop and then we would set some sort of
02:32variable that which would be an integer and we would usually start it up with
02:35zero. Now we're going to set how many times do we want to repeat this action
02:39and not my scenario I want to repeat the action 10 times so I'm going to say
02:43as long as i is smaller than 10 as we're counting from 0 through 10 it
02:47would stop at 9 which would create 10 times. Now you might be pondering now why
02:53am i starting from 0 and not from 1 as 1 is probably more intuitive to start
02:58with. The main reason is because flash and loops well flash for and loops or
03:06loops and for let me repeat that because flash loops and flash arrays are very
03:13very interchangeable you use many times arrays and loops and as such we want to
03:19make sure that our for loops correlate to our arrays and arrays and
03:25flash as we've seen in an earlier course their index starts from 0 and because
03:31their index starts from 0 it's quite important for you to try to always use
03:35your for loops and to start at 0 because you want to get used to that
03:38because if you don't it's going to start becoming tricky and you're going
03:42to start finding random bugs as you're working with for loops and arrays at the
03:47same time. Okay so with that really short snippet about that let's continue
03:53so we said we set our first item which was setting our variable and defining
03:57its value the second item we wanted to set it its rule its condition that
04:03we've met conditions before as well and the third one is actually our stepper
04:08so we want to step our I value one each time so I'm just going to I plus plus now
04:14that I got my for loop and I've opened up and closed the curly bracket I could
04:18do my action here the action that I want to repeat I could extract it to a
04:23function or I could just do it right ahead inside of my for loop so let's
04:27extract it to an external function I'm going to call an external function well
04:33not external exactly but out of our loop and we're going to call a do that
04:37the do action which was the name of our function we're going to open and close
04:42around bracket and then we're going to set it to void open a curly bracket and
04:46close a curly bracket and let's say for our action right now will be hello from
04:54a loop and if I run my application now we would see that our for loop should
05:01run 10 times and output onto our display 10 times hello from a loop to
05:07our output panel now if we wanted to we could have passed parameters in our do
05:11action we could have passed our eye right now we wouldn't if we would just
05:15type I obviously the eye is not defined so if I would try to output our for loop
05:20this way I would get an error because I is not defined right so we want to pass
05:25our I value through our do action so I'm going to pass it as a first parameter
05:30and by doing that now I have to set up my do action value so I'm going to
05:35call it index I could call it whatever I want and I could have called it I as
05:39well but it might be confusing so we're going to call it index I could have called
05:43it I because inside of this world this would be our I definition and it's kind
05:49of like separate variables actually because we're working with primitive
05:53values but let's call it index because it would make it a little bit easier to
05:57follow so basically we're passing a variable which is called I and then it's
06:03being captured by our do function and it's that value is put into our index
06:08integer thus now when we run our trace we should get our values that came out
06:15from our eye in our index value from zero through nine and this is really the
06:21most basic type of for loop which you'll use quite a lot in a lot of
06:25different scenarios where you know you just have to do something 50 times or
06:29you know you have to do something a certain amount of times and there's no
06:33point in duplicating that action now with that said sometimes when you're
06:38duplicating you're not duplicating one-on-one maybe you're duplicating and
06:42there's some sort of small variation and you could still treat it as
06:46duplication and one of the most common things I see that developers do and this
06:51is not necessarily a bad thing but when you think about it the more code you
06:57type the less readable it is to other people and also to you so yeah so you
07:02might write something that look that could look a little bit unclear right but
07:07even if it does look unclear that that unclear thing if it's only in two lines
07:14compared to 50 lines it's a little bit easier to cipher so as people try to
07:19figure it out or as you try to figure it out you just have to figure out two
07:22lines of code so for example let's say in our scenario we wanted to trace out
07:27three different values or two different values depending if the index value is
07:31is divides by zero by two or doesn't divide by two right so I could add here
07:39if statement right and we could break down and make a nice big if statement
07:43but let's use our ternary operations that we've learned in a previous release
07:48so if I do a ternary operation and I'm just gonna put that up here for a
07:53second and then we're gonna merge it together we're gonna see how
07:55complicated it looks like but it's not so we're gonna take our index and I'm
08:00gonna do an index model to which basically index model to would return to
08:05me a value of zero one because it's basically going to return to me the
08:09leftover when you divide the index by two that's the only possible values are
08:15one or zero and because of that we could straight ahead create a ternary
08:20operations because a ternary operation could expect zero which is false or one
08:25which is true so this works out really well for a ternary operation and let's
08:29say I did I have a left over I don't let's look at this one more time before
08:48we run it what's actually happening here one is we're creating a ternary
08:53operation and that ternary operation is asking first of all it's setting our
08:57condition our condition is basically divide the number without we got back
09:02by two and give us the left over so the value that's possible every time could
09:05be or zero or one so when one comes in the left over is one when two comes in
09:10there's no left over that's when one comes back it would trace out I have a
09:15left over when zero comes true comes through that means it's not true then
09:20it would follow up and only trace out I don't have a left over and as you
09:24recall the syntax is basically the question mark so this is the question
09:27mark and then this is the if condition and this is the else condition well the
09:34else value right so we could have typed it if we wanted if that value open
09:39bracket close bracket open bracket else open bracket put that in close bracket
09:45and do two traces but there was no need to it so if you run our application one
09:49more time now we should be saying I don't have a left over I have a left
09:55over I don't have a left over I do now we could grab now that we've built
10:00that we could just grab that part and I'm just gonna comment that up and instead
10:04of saying hello from a loop I just want to mix them together saying it's gonna
10:09trace our first our index and then that value of do I have a left over or I
10:13don't and if you run our application just one more time we should see that
10:17nine I have a left over I don't have a left over and as you can see although
10:25this is a very very simple might look complex but a very simple action even
10:30though we're duplicating even though we're using something to repeat it's
10:33very strong and very recommended to try to figure out ways for you to take
10:38something that's very similar but has a few different variations and a lot of
10:42times you could find those really simple modifications and cut down your
10:46code drastically and by cutting down your code drastically again you're
10:49making your code more legible to yourself as you get used to this style
10:53of coding and it makes your whole application smaller which makes it much
10:59easier for you to understand what's going on and every given time with your
11:02overall architecture of your application alright so that was our talk about how
11:08do we repeat an action using loops and how do we create those variations in that
11:13repetition and if you look back again into our initial video and initial source
11:19files and just look at the variations or even if you don't understand everything
11:23because they go a little bit more into the world of the visuals and movie clips
11:26a bit you could see that we're manipulating the data even though we're
11:30duplicating we're creating some variations such as repositioning the X
11:33and Y positions scaling the movie clip and so on and changing the color of
11:40course all right so that really is a wrap for things are related to
11:45repetition and probably the next step that we need to look into is how do we
11:49use loops to find things or use it for mathematical equations
