From Linkage to ActionScript
In the last video we created a new linkage and in this one we go over how we integrate this linkage into our code. By the end of this video we will learn how to create an instance of a library element by code and how to add it to the display(2 steps contrary to ActionScript 2.0).
Video Transcript
00:00Let's see how we could create an instance of our LMC.
00:04So if we go into our main class,
00:06we could just go directly into our main.
00:08What I want to do is I'm just going to create
00:09a quick little variable right inside of my definition at var,
00:13and I'm going to call it MC Oval,
00:18which the type of it is going to be a movie clip,
00:21or more accurately, let's start with the actual type.
00:24So the actual type is going to be an LMC Oval,
00:28which was the name that we put inside of our linkage.
00:32Then I could set a new MC LMC Oval,
00:39very similarly to the way I created earlier on in
00:43a previous video when we created an object in
00:47a previous course where we could create a new object setting,
00:50a new object. So in the exact same way,
00:52we could create a new Oval,
00:54and from Flash's consideration,
00:56we just created now that instance in that library.
00:59Now, if we run our application,
01:00we're not going to see anything because we
01:02never added it to the stage,
01:03and we see that there's a little typo here.
01:05It could not find the constant LMC Oval.
01:10That's because there's an E here that shouldn't be fantastic.
01:16So if we run our application,
01:18we'll see that it's running,
01:19but it's not showing us our Oval.
01:21The reason is because we have to actually now,
01:23because it's not on the stage automatically,
01:26we didn't place it on the stage,
01:27we have to tell Flash not only that we want to create
01:30that instance, that we want to also put it up onto the stage.
01:35The way we will upload our instance onto the stage is quite easy.
01:38First of all, we want to know where do we want to add it to,
01:41and right now we want to add it to directly into our root or our main.
01:46So we want to add it to this,
01:47we want to add it right ahead.
01:49So I don't have to write anything
01:51because our main extends a movie clip class,
01:54and the movie clip class is,
01:56and if we just jump for a second into our chart,
02:00we could see that our movie clip and also the sprite
02:03are all children of the display object container,
02:06and as the name implies,
02:08that means these are display objects that could have children.
02:11Well, a text field can have children,
02:13a simple button can have children.
02:15Anything that extends this display object container could have children.
02:19So our stage has children obviously,
02:21because we're in the stage and we're adding content onto the stage.
02:25Our sprite could, we haven't worked with sprites yet,
02:27but we're going to look at that later.
02:29Our loader could, we didn't work with those yet either,
02:32we're going to use that to load external visual elements.
02:36And we have our movie clip, which we are starting to really work with,
02:40although all this time we were working with movie clips
02:42because our main class was a movie clip always.
02:46So because it was a movie clip,
02:47we were able to just drop things onto the stage
02:50and Flash would then know that it has children.
02:52But instead of doing it dynamically,
02:54now we actually want to actually explicitly tell Flash.
02:57Well, now we want to do it dynamically.
02:58We want to explicitly tell Flash, add a child right now.
03:03And the way we do that is, as I just said right now,
03:05that's actually the key.
03:07So add child is the name of the function,
03:10and add child expects one parameter,
03:12which is the display object that we want to send.
03:14Now, don't get confused because you don't want to send the class itself.
03:17LMC Oval is not a display object.
03:21It's a class that when we instantiate it creates a display object.
03:26In our scenario, it creates our Oval that we saw in that library.
03:29So MC Oval is our instance, is actually what is visible,
03:33is actually the item that could actually go onto the stage.
03:37Now, if I'm trying to explain this in a way
03:39that's a little bit more visual,
03:44we could say that we have a recipe, all right?
03:48We have a recipe for bread,
03:50but we never created that bread.
03:52Although we have a recipe, we know how it's going to look like,
03:55we have pictures of how it's going to look like,
03:57like the stuff that we have in the library.
03:59But we never created it.
04:01And only when the point in it,
04:02and let me just move my face to the side a second,
04:04only when we created our new LMC Oval in line 10,
04:08we actually told Flash, create an instance,
04:11create a new visual element, which is an LMC Oval.
04:15But we didn't tell Flash where to put it, right?
04:17It's kind of like in the middle of the air right now.
04:18It's just a variable that's hanging inside
04:21of the scope of something,
04:23but it's not connected visually to anything yet.
04:25And we have to tell Flash, okay,
04:26now after you've created this visual element in line 11,
04:30send that visual element to myself in this function.
04:34What is myself, which is basically indicated just,
04:38I could have said this instead of that as well, right?
04:42So if I said this, so if I said this,
04:45or I didn't say this, it's explicit.
04:47It's implied that I'm saying this.
04:50So if I say add child, it's actually gonna ask main,
04:53the instance main, which in our case is the root.
04:56It's gonna ask our instance main
04:58to add a child onto its display, all right?
05:02So far, so good.
05:03So if I run my application one more time,
05:05we should be getting our class right onto the stage.
05:09All right, very, very good.
