Learn about creating methods in this Flash Professional 8 advanced next level training video series.
Tags:adobe,adobe flash professional 8,create,creating,flash 8,macromedia,methods,next level,total training
Grab video code:
Transcript
Now let's go back to our class, let's try a couple of other new things. We have dealt with variables, both public and private. We have got a Constructor now. How about a method? Well, we can create methods; there are simply other functions included in the class. We have got our Constructor function, that's special but I can go down top the bottom of the class and before we close our class out here, let me just add under the line.
We can add another function and this will basically be a method that we can call from our class. I am going to stick with our quotes that we are using right now. I am going to make a function, called quote and this function is going to be very simple. We need to construct it as a function, so I will build it up with space for parameters and some open and close braces for grouping the rest of the structure and what I will do is have this function simply trace an interesting quote. Here I think, we will use 'forescore and seven years ago...' and I think I could probably stop there.
Now, that's pretty simple function but you can see that it's actually going to execute something, when we call it; let's try calling this. I want to save my class; we will go back to newClasses, down at the bottom here, let's add a line and we will access our newThing object and call our new quote method.
So we will call the object, newThing and call the method, quote and let's see how that's going to run. I will test my movie once again and you can see that it executed the call and executed the trace statement here. I think just to push this one a little bit, we could make our quote function have a parameter in it that would specify the quote and that would actually be pretty easy at this point. Let's set that up. A minor modification to our quote function. I would have to pass a parameter into here, just like any other function.
So I will call this quote1 as my parameter and whatever it comes in, we will simply trace it. Now that means my method is going to be expecting a quote1 parameter, so we will save that into our class description. We will go back to newClasses and when I call the quote, we will put something clever in here; a very famous one, ' I've fallen and I can't get up'. Alright, let's execute that and you can see that we are again getting the trace of our quote dropped in there, so it's pretty easy to build up a very simple method system and add it to our class description.
Now, as we go along, I hope you are seeing a lot of common items that we have seen with a lot of the classes that are built into ActionScript. Obviously, our classes have properties, they have Constructors, they have methods; some of the methods have properties, some don't. We have also seen a few classes where the Constructor itself has a few properties. The Tween class comes to mind here, whenever we instantiate the Tween class, we don't just say new Tween, we add in a whole bunch of parameters that specify all the properties of that Tween.
Now, I would like to try that on a very limited basis here. Let's add a parameter to our Constructor. Now I am going to go back to our class and we will set it up, so that we can add a parameter right inside this Constructor function. It's not much different than adding a parameter inside of our methods as we did before, so let's add a message2 parameter.
Now we could do a lot of things with the parameter. We can use it directly inside the constructor or we can use it to assign properties to the class itself. So let's make message to a property. I will just copy our public variable from message1 and paste it in and we will change it message2, and now our class will have a brand new property, message2.
Now obviously, we are not assigning message2 here, very much like I saw in message1 inside of the Constructor, let's also assign message2. The big difference will be, we will assign message2 from the parameter. Now I will use this.message2, which is referring to the property of the class and set that equal to the message2 that we are receiving as a parameter.
Now it sounds a little confusing here and a lot of people, specifically don't use the same name for the parameters they would for the property. I am also going to do one other thing and I might even go ahead and fix up our method that we created earlier. It's a very good habit to get into, when you start specifying parameters for anything in a class, to also go ahead and data type that parameter.
So let's fix things up by data typing this one as a String data type and we will do the same thing down here for quote1. When you start reusing this class later, you want to definitely make sure that you don't pass the wrong type of data into the class, otherwise the class won't know how to handle it. If you specify the data type when you call it from your movie file, if you were to pass a number into the quote method, you would get an error as a response right away, letting you know that you need to modify your parameter.
Now we have got it set up, so that our Constructor has this additional property that we can assign, that's going to assign message2. While we are at it, let's go ahead and have our Constructor trace the resulting property that's assigned from the parameter value. That way, we will be able to test it right after that.
Now I have assigned the trace message into the Constructor, so we should see our message2, along with our trace for message1 and our trace for stageWidth. Let's save this into our class and let's go back to newClasses and we will do a quick modification. When we call our Constructor up here at the top, I would like to add in a parameter for message2. Let's just add in another quote, 'this is fun!'. I guess I should put an exclamation point since it is and we will give that a try.
Now we have added that parameter, dropped it right in and it is a String, so it's correctly assigned to the right data type. Let's test our movie with that modification. Now, look up at the top, we can see message1 being executed, our trace is a stageWidth and there is message2.
Now, the benefit of adding a parameter to the Constructor is we can not only assign parameters at the time of construction, but we can assign different parameters to different objects. As an example of that, I would like to create another object, I am going to save myself a little bit of typing and I am going to go down here to the bottom and I am going to instantiate another object, but I will just call this newThing2 and we will send in a different parameter.
We will just add another in our long line of quotes, we will set this, make this little bit more Shakespearean, ' to be, or not to be'. Now we are creating a brand new instance of the object newThing2. All the other properties are going to be the same, except for the one that we are assigning in the Constructor.
Let's test our movie out. Now, we have got our construction for the first one coming down here, you can see I am running the Constructor a second time; after all the trace statements, we get the sane thing for message1, the same thing for stageWidth, but now, we are getting two different quotes from the assignment of our Constructor.
Comments