Learn about building custom classes from the ground up in this Flash Professional 8 advanced next level training video series.
Tags:adobe,adobe flash professional 8,class,custom,custom class,flash 8,macromedia,next level,total training
Grab video code:
Transcript
Now in this lesson, we are going to be taking a look at building a custom class from the ground up.
So, we might as well just get things started by building our class itself. Now, I have got a file here called newClasses.fla and we are going to be using this to host the classes that we build, but we are going to be meeting some other files in order to create our classes. So, I am going to get things started by going right up to the File Menu and choosing New.
Now, you have probably seen the New Document Panel a lot and you might have even wondered why you might use some of these other different types of files? We are going to be using an ActionScript file, and what this file is for is for just writing ActionScript in it. This kind of file will be a .as extension and it will simply be a text file. You could actually open it up in any text processor you want. Now, that means it won't contain a Timeline, it won't contain a library, it won't contain any other graphic elements that were used to seeing in our Flash files. So let's click OK on ActionScript and we will see that all of those other panels here are grayed out and we basically just have a big script panel.
Now, I will even point out inside of an AS file, you won't be using the Actions Panel either. You will simply be using that main big script Window. It does still have the Reference Panel on the side of it. So, you can still access all the same reference objects that you were used to using in your ActionScript Panel. Now as far as the class definition itself, we are going to need a few things in order to make a class. First of all, I am going to need something that calls this out as a class. We will start it right off with the class definition. Right after the class definition, we need to specify the name of our class. Now, naming conventions for classes are pretty simple. Most of the rules stay intact from all the different kinds of namings that we are used to using in ActionScript, but one thing is a little bit different, it's customary to use a capital letter starting of a class. Now, we have seen that in using the Math class, the Mouse class and the Key class inside of ActionScript. We are accessing the class directly. Well, this is going to be a little class that we are going to call SomethingNew. Notice I started it off with a capital S.
Now, I am going to setup a couple of braces and these are groupings just like we are used to and inside of these braces will be everything that we used to define the SomethingNew class. At this point, we can actually stop and test it because this is all it takes to create a class. We need a class definition and a title. Now, in order to use this class, I am also going to need to do a couple of other things in saving the file. So, let's take our Script 1 file that we have built and let's do a save. Now, I am going to put this into the Lesson 03 folder and that to be right next to our newClasses.fla file that we had opened at the start of this lesson.
Now, the last thing we need to do to and make sure this class is going to work is we need to name the file with the same name that we used for naming the class, in this case, SomethingNew. Now, just like everything else in ActionScript, your capitalization is important. So, you notice that I typed it exactly the same way as my class was subscribed. That's it; we actually have a new class. Now, let's see about using this class and see what it is going to do in our Flash files. I am going to switch back to our newClasses.fla file and I am just going to go to the actions layer in that file on frame 1 and let's write some actions into here using our new class.
We have instantiated a lot of classes so far so I am going to do this the same way. I am going to use the var keyword to scope it. I am going to give my object a name. I will call it newThing, and I am going to type this object. Now, here's where it gets fun. I am going to use our new class, SomethingNew. I will need a new keyword to instantiate it and I am going to call off the SomethingNew Constructo. Aand that's it, we should have a new object. Now, we won't see very much because their object of course is empty, but let's see if we can at least verify its existence. Now, if I just do a test movie at this point, I probably won't see anything except what was on the stage when we started this up, but I could go into the Debug Menu and list variables. Now, let me pull up my list here in the Output Window. Let's take a quick look. We have got a lot of different pieces in here, but right there at the top, there is a new object in the SomethingNew class and you can see that it is type SomethingNew. So we could see that our object does exist. Of course, we are going to have to fill it up with some things in order to make it useful but we can build it very easily.
Let's just close this up here and we will go back over to our SomethingNew class and let's add some stuff to it. Now, probably the easiest thing to start with is property. As we know from our class definitions a property is just a variable but it's attached to a class, so I am just going to instantiate a variable inside here and since our class is a little free form, I am just going to make a couple of different variables that we can use to test this out.
Now, I am going to make a variable called friendlyWelcome. Now, I am going to type that as a string and we will just set that equal to some welcome like, Hi there, and so it's friendly so I will put a little smiley at the end and we have a property which is a variable inside of our class and it has a value, Hi there. Now, let's go and use that like we use any other property. I will save my SomethingNew class. Saving our class is actually really important because we won't see any changes when we pull up the class or when we instantiate it during runtime, it's going to pull up the current version of the class saved onto the hard drive. So make sure you save it.
Now, let's go back to newClasses. We have already created our object and now we know it has a property. So let me just go down to the next line and we will do a trace. We can trace out the property of our newThing, that's what we called it and the property that has is friendlyWelcome. Once again, just like any other property on any other object, let's test our movie. And you can see that we successfully traced the value of our friendlyWelcome property. Pretty easy so far!
Comments