02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is

The Breakdown: Building The Mini Translator Tool

Building out the translator tool. based on our Quickie building and explaining all the lines in the code.

Video Transcript

00:00So before we start really digging into the code, let's first look at the different objects we have on the stage
00:07The first object that we have on the stage is actually a simple text field
00:10Which is a dynamic text field and if we check in our properties panel, we'll see that we called it our text field a T blurb
00:19So far so good. So we have a little text field. We have a small little background that has no
00:25Meeting. It's just a little background to keep things
00:28Interesting and we have here a lot of movie clips which they're on the wrong layer. So let's just grab them
00:36I'll just cut them out and just put them on a
00:40buttons layer
00:42And I will paste in place
00:45Okay, so let's look at our buttons
00:47basically what I've done is the simplest way which is duplicating a lot of movie clips and creating a new name for each movie good and
00:55and the steps to do that would basically be if I would delete six I would just
01:01Go, I would just click on the alt shift
01:05Duplicate my movie clip now
01:07I have a separate instance and then I would right click and create a duplicate symbol
01:13but in our scenario because we have have it already I could just swap the symbol and
01:17Swap it to our day six movie clip that I've created already in the library
01:22So we have here are six different days
01:24And now I need to make sure that the name of my movie clip matches what I need and we'll see that the names
01:30Correlate so there's MC 0 MC 1 3 4 5 and now because I've created six again
01:37My MC 6 will be called 5 so I'm just gonna make sure that it's called 6 and basically all
01:43Reason is because we wanted to create a easy way for us to recognize what that button is
01:48So we created a name that correlates to the copy within it
01:51And the same is true for our buttons with our e button which we've called
01:56MCE which is an instance of the MCE movie clip and
02:00It stands for English. We have our French button and we have our Spanish and Italian button
02:07And I'm just going to uncomment the more because we're gonna build our application straight out knowing how to work with four languages
02:15So now that we've seen the assets on on the display
02:19Let's go into the code and start looking step by step how we actually built how we built out this little application
02:26So click on to your properties panel when nothing is selected and change your main short if it's main short to be main
02:34starter
02:36Main starter and just click on your edit definition and there should be a movie clip ready
02:41Waiting for us to start working with as it as you can see right now. There's nothing here besides a
02:48Little bit of the languages so you don't have to search it up, and we don't have to type it up
02:52The first thing that we want to do now that we actually have that
02:55Have all our assets on on the stage is actually create our object that is going to contain all this information
03:02So what I'm going to do is I'm going to create a private variable
03:05And I'm just going to call it Oh link because we're going to use that as an object
03:10Which we're going to then link the different languages to and now when I'm starting up my main starter
03:16I'm just going to type Oh link
03:19Equals, and I'm just going to create a new object so now I have a new object and my new object doesn't contain anything
03:24But we want to start populating it with the information. That's inside of our the different languages because we want to be able to
03:32reuse our object throughout the application
03:36so as
03:37As you've noticed before when we're looking at the different languages if I just hop on to it
03:42Every language is just represented in the movie clips as one separate
03:46Name and because we know there's only one separate name, and we know there's an e f s I
03:52We could just then in our object itself create different
03:56Properties and put inside of these properties something right we could put let's for now. We're just we'll create the English one
04:03And we'll create the French one
04:06and
04:07We will create the Spanish one
04:10And last for this sample. We're going to create the Italian one as well now
04:16The naming really is up to you
04:18You can name it whatever you want, but what we tried to do is to create a an easy connection between our
04:24Buttons that are on the stage and our data and by me creating that connection
04:29We're going to avoid the need of using if statements, or we're going to limit it to a really small amount
04:35Now already have your pre-populated actually the data
04:38We want to contain for every language which is the days of the week
04:42So let's instead of putting a string into our separate
04:46And they're separate variables that are inside of our object our several properties. Let's give our English property
04:52I'm just going to copy that array
04:53I'm just going to paste it right in and now our English has an array of seven different values
04:58Which are the days of the week and let's do that for French as well
05:02And all we're really doing is providing our object with an array that gives it that our object all the information needs to know
05:13what
05:14The what the value is for every language so before we even go into actually building out all the code now that we know that
05:21It's inside of an array. We know that
05:23English in position position zero will have the same value as French in position zero and
05:29So on which will make it much more simple because then we don't have to go back and forth with the logic and if and
05:34else and figure out
05:37When is what and this is really the heart of the application so whenever you're building out an application and you're thinking about
05:43How am I going to get rid of if statements the heart of it is could you massage the data that you're getting?
05:49Could you build it out in a way that will be very easy to access?
05:53The more even if this object becomes really complicated for human. It's not very human readable
06:00It makes all the rest of your code very human readable
06:04Which is the price that is definitely worth taking because it's just a couple of lines and maybe it takes a little bit
06:10To wrap your head around it because what we're doing here is a little bit complex
06:13There's an object that has properties which they then contain arrays and there's a relationship
06:18We thought between all these arrays because they share the same values and the different there are different index values
06:25Okay, so we created our we created our different our we created our object and the next thing we want to do is actually
06:32before we even start
06:34Doing anything else what we'd want to do is and I am just gonna
06:38Push this aside a bit. So it's out of our way. Oh
06:43Maybe we'll give it one line there. Okay, let me just type it for so we could get a nice big big screen
06:49the next thing that we want to do is we want to actually listen to our mouse events now we don't want to listen to
06:54one specific mouse event because we have
06:58Seven plus four different buttons on the stage that we want to listen to
07:03So the easiest way for us to do that is we could just basically just call and add event listener
07:09Directly to our main movie clip which contains all these movie clips. So automatically it would be listening to those click events as well
07:16So what we'd want to do it already imported the mouse event
07:19So I'm just going to add an event which will be mouse event click and we're going to listen to on click
07:27We're going to call our listener on click
07:29So now that we created our listener we have to create our function our private function in this scenario and
07:38We will call it on click because that's what we called it
07:41and it would accept one parameter, which is the
07:45Event well the mouse event ramp and we have our function
07:53So we have our function and we have our on click event
07:56The next stage that we want to do is we want to actually find out what was the button that was clicked on
08:02Okay, so let's see how how that would work first of all
08:07Let's let's start up by creating a small little variable so we could reuse it. We'll call it our ID
08:12We'll call it a string will we'll call it ID our string and let's see how we're going to get there
08:17And before we even continue on let's do a little trace right here
08:20What I want to do is I want to take our target
08:24Okay, and our target is always going to be the movie clip that was clicked on
08:28So if we're going to click on one
08:30Our target would be that movie game now
08:32It's hard to see that so what we could do is now take on to the next step
08:36Which is basically asking for our movie clips name, so you know type it for a second here
08:41And I'm going to ask for the art of wakeups name because we explicitly gave our movie clips names
08:46And now we'll see that in our output. We're getting the movie clip name
08:50But we don't care about the first two characters
08:52So let's get rid of the first two characters and only focus on the last character
08:56which is what we really care about and the way we do that is
09:02By using first of all we want to make sure that our name even though it is a string
09:05We're just going to be we're going to play safe
09:07We're going to type to string make sure that we're working with a string even though we know it's a string
09:13And the next step is we want to slice out we want to slice
09:17What do we want to slice we want to slice out the first two characters?
09:20So I'm just going to type two so we could start from we're going to slice and take out to and onwards
09:25basically
09:27From position two which is zero one two from position two and onwards we're going to get everything
09:33So we're slicing out everything from position two and just to be safe
09:37And not only to be safe because we're using here everything in lowercase in our links
09:41We just want to make sure that everything we get is in lowercase
09:44So we're going to just add one more line to this and ask it to be always
09:49to lower case
09:52And I'm just going to close that bracket
09:54And let's run our application. Let's see what we're going to get now
09:57So if I click on my buttons now you're going to see that now my buttons are actually giving me exactly what we wanted to
10:02Give back. It's giving back to us
10:05Just the ID that we want from our movie clip
10:08So now that we know our value is valid we could just grab this and just put it inside of our ID
10:13ID and now we know that what was clicked on is in this ID
10:17So another we finished one more step
10:20Now let's take one step back and go back into our main starter now
10:25We know what's being clicked on before we even start playing around with the different click events
10:30Let's now just configure some basic things so I'm going to create here one more variable and go up here to the top
10:37I'm actually going to create two new variables or actually even three. Let's just get all the variables out of the way
10:42So first of all you want our Y base
10:45Which will be an int what is our I base our Y base is sorry Y base our Y base is basically going to tell our
10:51Movie clips when they're clicked on
10:53What was their initial position and because all of our movie clips are in the same Y position if we'd look at for a second
11:00in our FLA
11:01We'll see that all our movie clips are positioned in the same Y position
11:06And that's great
11:07So we could use that for advantage is just grab their original Y position and share that information
11:12Every time we click and we move the button to make the user see that that item was
11:17selected
11:18So what we'd want to do after we create our Y base is we just want to make sure that we stash that
11:24Data, so we're just going to make sure that Y base has a value of doesn't really matter which of the movie clips
11:29So I'm just going to select MC 0 which is just one of the movie clips
11:33And we're going to grab its Y position making sure that our Y base just has one of the Y positions
11:38Let's see what other variables we want to create here. The next two variables that we want to create here are
11:45two variables one is the last digit and
11:49The other is the last language. We just want to make sure that we always know
11:52What was the last digit that was clicked on so to do that?
11:55We're going to create two variables private var last digit which would be a movie clip and we're going to create another
12:04Private var which will be our last
12:09Lang which will be for our last language that was clicked on and as we've seen in our application again
12:14We have four different language buttons and we have seven different buttons for our digits
12:21The next step let's set our well
12:23Let's start but let's go back into our on click now that we have all the different pieces of information that we need
12:29Let's go back into our our on click and let's see what would be our next point
12:36So the next point first we want to check
12:38Now that we know that we have our ID we have references to our last digit last thing even though we have nothing in them yet
12:45The next thing we want to do is first we want to check one if statement and all we really want to ask
12:51all we really want to ask is if our
12:54ID value exists inside of our o-link now our ID could be EFS I or any other value
13:01How did we check that the easiest way to do that? We could just check if it exists
13:05So if I just type o-link and I just paste into it that ID
13:12It would basically would return. Oh, I closed the bracket in a wrong way. Here we go. Nope
13:18And let's have your little else
13:20So if it's inside the ID, we know it is
13:23One of our languages if it's not it's probably one because right now you only have those buttons on the display
13:30It's probably one of our movie clips that are from zero to six
13:35Let's test that out. Let's see first of all if we're really getting it
13:39Lang and we'll test on our else
13:42digits
13:44And if we run our application one more time and we click on one of the digits
13:48We'll see that all the digits return the value digit in the trace panel
13:52But if we click on the E or the S or the I will see that it returns to us a lang
13:59So we know that now we know that our ID exists. We know that we found it inside of our application
14:07Now the next thing that we would want to do is first of all, we know we're inside of the lang, right? So
14:15What we'd want to do it now is we want to make sure that our last lang
14:22equals E
14:25Target and we'll just make sure that it's as a movie clip because we saved it as a movie clip
14:31So we're just saving our last lang as our movie clip and probably would do the same thing in our else statement
14:37So in our else statement, what would we do in our else statement? We would say our last
14:42digit equals E target as
14:45Movie clip because the only chance that our if statement will happen is if it's a lang
14:51So now we know this is the last thing that was clicked on if it's not then it's probably one of the last digits
14:56Thus we want to save it and remember what our last digit is
15:02Okay, so let's see. What's let's see. What is the next step now before we even change layout?
15:07Let's start working on the copy. So now that we know what the last language is and we know what the last digit is
15:15We have only one problem before we could start building out and adding our copy in is that initially when we run our application
15:22the site doesn't know what language it's running in and
15:26Because it doesn't know what language it's running in we can't really tell it what language if someone will click on the digit
15:31We wouldn't know what language to tell the application to display
15:36So the way we're gonna solve that problem is we're gonna just tell the application even before it starts our click event
15:42We're just gonna tell our applications
15:45We're just gonna tell our application. What is the language we want to start with and in our case
15:49We want to start with the language English, so I'm just gonna type
15:52last
15:54Lang equals mce because mce stands for our English and we want to do the same thing for last
16:00Lang or sorry last digit
16:02We want to say that those the MC 0 so if no one click if the user never clicked on anything
16:08he would automatically get the value inside of
16:11Position 0 if the user never clicked on a language that you automatically get the value that's inside of English
16:17So now that we know that there's always going to be a value in last Lang last digit when we get to us in
16:22Our we could click on something we can now move on to
16:26Outputting the copy so now that we could build out outputting the copy
16:30We let's just create a little function here a little private function because we don't want to share it with anyone besides ourselves
16:36and we're gonna call it
16:39We are going to call it set copy and
16:47Set copy doesn't need to accept anything because it's going to be basing itself off of the values of last Lang and last digit
16:54And the next step that we want to do and it's going to look a little bit complicated
16:59So before we even break it down before we break it down and actually output it
17:04We've seen how to work with our and I'm just going to copy this line right here
17:08We've seen how we get fetch our information from there e target. It's a very complicated long line, but it's a very powerful line
17:15So let's see how we could grab the same information. I'm just going to paste it in twice
17:21But this time around we don't have an e target, right? What do we want to get?
17:25We want to get our last Lang and instead of our e target on this one. We want to get our last
17:31digit and
17:32If we run our application, but before that we want to make sure that when we call on click
17:38Our set copy copy will be called because not it will never be called
17:42So if we run our application and we click on one of the buttons
17:46We should get a value of our two IDs first. We know it's English and we know it's position number two
17:51So now that we have this information
17:54The next step the next step is very simple
17:57the next step is actually us accessing and our information that's inside of our o link and
18:03Let's see see what the logic so first we know our first value that we're getting back here is the language and
18:10That's basically the link object that we want to get into and we've seen how we've used it in this line where we checked
18:16If there's something in our o link language
18:19so if we now just add one more trace here and
18:24I'm just gonna type o link and
18:27We're gonna grab the value that we get from here and just paste it directly into our object now
18:33This definitely looks complicated. So let me just highlight then the inner area and
18:37We're basically asking for the same thing that we've asked on line 30
18:41But now we're sending it to our object and asking our object to output whatever it has
18:46So if you run our application one more time, we're gonna see that now when we click on two, it's returning to us our array
18:53Which is great. We're already almost there
18:55The last piece that we're missing is actually telling it what piece of the array do we want and we in the scenario looks like
19:01We want position number two
19:04Because we click the number two
19:06So let's see how we get that information
19:08That would be now that we're now that we know that we're getting back an array
19:12We could now call our array and tell it what position we want to go to right? What position do we want to go to?
19:19Exactly, we want to go into the position of our second value, which is our last digit that was clicked on
19:26So if we run our application one more time now that we've got our last digit as well
19:30We'll see that when we click on number two
19:32It's gonna tell us that two is Wednesday because we're in English and three is Thursday and one is Monday. Well zeros Monday, sorry
19:40So now that we got that information
19:42Let's now just take that and just spit it out directly into our t blurb that we've met earlier
19:48So now we don't need all these traces and instead of these traces we could just say t blurb
19:54dot text equals
19:57You could just delete that because we're not doing the trace and if we run our application one more time now when we click on
20:03the different values
20:05It's gonna give us the days of the week and our language is
20:08Application is almost work working now. We could change languages
20:12Everything seems to be working only there's no indication to us to know
20:16Where are we at any given time and another problem that we have is when we first run the application even though?
20:22We're in English and zero. We don't see it
20:25So to fix that problem what we want to do right now is I'm just gonna grab our on our set copy function
20:30And I'm just gonna call it as
20:33the last thing we do before we end our
20:36constructor function
20:40Making sure that when we run our application, it's gonna output immediately that it's Monday
20:46Now that we know now that we have that information
20:49We want to make sure that our buttons now actually highlight and show the user
20:53Where we are in the application in every any given time and to do that
20:57We want to start adding here a little bit more logic
21:01So what I'm just gonna add a little bit of space here
21:04and this logic is already quite simple because we know what our last lang is and
21:10We know what its why was but we want to make sure that we were going to give it the base y the y base
21:16Making sure that we don't go more than that and we're just gonna add 10 to that value
21:20and I'm just gonna duplicate this line and
21:24I'm gonna do the same thing for the last digit and just to keep things organized
21:33I'm just gonna grab this information and just put it all together
21:36So we first set our language to be the last lang and we set our digit to be
21:42MC 0
21:43Okay
21:44And now we're almost there now as soon if we run our application one more time now
21:50We're gonna see that initially we're up. We're exactly where we're supposed to be
21:55But we're missing now that when we're clicking around the rollover zone update because in on in our on click event
22:01We're not making these updates as well
22:03So
22:05Let's see what we basically want to do
22:07So the first thing that we want to do is we want to first reset our last lang
22:12Right, we want to reset our y position to be y base
22:17Making sure that whatever was clicked on whatever was last click on will reset and same is true for our last digit
22:24we want to make sure if the last digit was clicked on and
22:28Notice how I'm doing that before we change the value in it
22:30So we want to take what was in it already and set it back to its default
22:34We want to take what was in already in the digit and set it to the default making sure that when we click on the next
22:40thing our
22:41Previous movie clip sets back into place when we click on the previous thing our previous movie clip sets back to its original position
22:50So now that we've got that sorted out
22:52Let's see what last line we're actually missing in our application to get our application completely running and the last last line that we're missing
22:59is we could have gone inside of our application and basically just duplicated this line of code and
23:05basically duplicate it right here and
23:08Duplicate it. Let's see what this is doing and duplicated right here as well
23:13And that's completely valid code what basically we're doing here resetting the original y we're
23:19putting a new movie clip whatever was clicked on enter our last thing and then we're
23:24Adding 10 to the value of the base base plus 10 making sure it will get pushed down and definitely if we run our application
23:30It will work and there's nothing wrong with building it out in this way
23:34but I just wanted to cut down the amount of quotas these two features are basically the same thing and
23:39I could leverage this information and I could delete this and basically add after our if and else statement
23:48So we want to make sure that our e dot target because we know that it in any case
23:53Whatever was last clicked on we want to push it 10 pixels down and the way we do that we could just set it as y base
24:00Plus 10 and now we know that our application will always when something gets click on get push forwards and we'll always reset our previous
24:08Last laying in last digit. So if we run our application, we'll see that now we have a little mistake here
24:14Let's see what's going on here
24:16And we see we have a little oh we didn't type e target y and we all try to override our target
24:23Which we were not allowed to do that. So we run our application one more time
24:27Now there's no errors and we'll see that our application always pushes down the value as we click on it
24:34Perfect. Our application now works
24:36Now the last thing I really want to talk about our application now is exactly all we really need and there's nothing left to add
24:42At this point, but I wanted to point out about these objects now when you
24:49Start up an application
24:50maybe you won't know what the object you want to create and that's a bit of the skill set of building up and figuring out
24:56when to use them and how much to use them, but I
24:59would say the more you use them the better because
25:02The more of these linkages you create as long as they're organized and they're centered in a place where people could find them
25:08And you know, it's clear what's going on
25:11It will make your code a lot less complicated and with time
25:15You're going to develop the skills of figuring out how and what structures you're going to create because sometimes you're gonna have
25:22Multidimensional there's there again, it could get really complicated
25:26But what how don't no matter how complicated it gets it's going to make your application more simple
25:31Which means it's going to make your overall coding more simple
25:34So I highly recommend to always when you started an application always and you're working on something to think twice
25:41Before you start using if statements, especially when you start using cases almost 99% you're you could avoid using cases
25:49cases are there it's it's almost
25:53It's very very rare where cases are actually needed
25:56There's definitely scenarios where they are
25:57But a lot of times we could solve the need for a case or switch just by using objects
26:03So that was it for our for us for this quickie and we hope you enjoyed it
26:07And we'll see you in our next video that you choose to watch

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!