Now let's go back to our ActionScript Panel and let's take a look at some other objects in here. Now we were looking specifically at movie clips and text fields, mostly because everybody has worked with those a lot already in their Flash productions, but let's take a look at some of these other classes, like for instance, the Mouse class.
Now the mouse is an object that we are not going to actually create instances of, we are just going to use the mouse object. You will notice that it has methods and listeners, but it doesn't call out anything specifically as an event handler. I will pull-open the listeners and you can see some very typical mouse type of events, that we have already got set up with our Mouse class. Let's try to use them.
Now you can see that it does say onMouseDown, but since it's not classed as an event handler, it's not going to work the same way. We could give it a try; we can go over to our Actions Panel and add a little handler for the mouse. Now we are going to be directly accessing the Mouse class, so I will type in Mouse and let's try to add an event handler for that. I can try onMouseDown; we will set it up as a function. Of course, I will need to add some braces and we could set some simple kind of trace thing in here, if we expected the mouse to be pressed at this point I could just put trace("mouse pressed").
I really don't even have to try this, I can just do a syntax check and I actually should get an error message on here. If we take a look at the error that's reported in the Output window, pull this over here a little bit, just a little bit wider so we can see the end of the message, there is no property with the name 'onMouseDown'. Now it's looking at our dot (.) syntax and it's actually looking for a property instead of an event handler. So you can see that's not going to work. Now let me close my Output window and we will comment this out.
Now in this case, the mouse is actually broadcasting the onMouseDown event. In the Listeners category, we have got a list of things that we can listen to from the Mouse class. So we are going to need some kind of other object to do the listening for us. Now to try this out, I am just going to create an object that we are going to call a listener. I will use my var keyword to scope it to the Timeline and I am just going to call this object mouseWatcher. Now the type of this object is just going to be Object. Our main object class is actually good in this case because it doesn't really have any of its own properties or event handlers as well, but it's a dynamic class. So we can add lots of things to it.
Now I will need a new keyword to instantiate it. Of course, we will go to our object constructor, "Object ()" and we have got a brand-new object. Now notice there is no specific class of listeners. So we are just going to make this into a listening type of object. Now if I want this object to listen to mouse events, all I need to do is set up something very much like the handlers that we've built before. I am going to take my mouseWatcher object and I will do an onMouseDown. Now we are going to write this up equal to a function, just like we would in normal event handler. We'll need an opening brace and a close brace.
Transcription by:
Scribe4you Transcription Services