02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Dispatching Events

In this video we take a break from working with time and look at Events. Our goal is to dispatch an event when our time out is completed so we can then let our main class know of the changes. To do this we need to extend our class to be capable of dispatching events(extends EventDispatcher).

Video Transcript

00:00So our timer now works. We have a timer that's actually delivering what we promised.
00:05We have a timer that accepts a certain amount of time, and then it starts counting down.
00:09Now we have only two problems with our application right now.
00:13The first problem that we have with our application is that there's no way for our application to know when the time is over.
00:21And the second problem that we have with our application is that there's no way for our application to know that there has been an update.
00:29Now, if we wanted to create some sort of API that enables us to actually see what's going on,
00:34because these traces are really not the real world, right?
00:38We have to add something more into our tool build.
00:42We looked into events and you might have watched some of the more advanced videos about working with events,
00:47but we haven't really worked with events within our visual display yet.
00:51What I want to do is revisit that we were talking about, and even before we go too deep into the world of object-oriented programming.
00:59Like we said, just as we worked with our application and we're basically extending the movie clip in our app,
01:06that gave us capabilities. That gave us access to add children to the stage.
01:10It gave us access to a lot of stuff which we couldn't do if it wouldn't be extending the movie clip.
01:16Same comes to our timer.
01:18Because our timer is not extending anything, we have really limited functionality.
01:22When I'm saying it's not, it actually is.
01:24It's by default, we said it's extending, extends object.
01:30So by default, even if you don't type anything, your class is extending the object, especially if you're not doing anything.
01:36But what we want to do, we want to extend something that enables us to dispatch events.
01:40Just as we were listening to events so far, we want to be able to say, hey, a new update happened.
01:49The digits just changed. Or we want to say the timer has just completed.
01:54So let's see how we could do that.
01:56The easiest route to do that, we first want to extend our timeout to extend something that dispatches events.
02:05And lucky for us, there is a class that's called event dispatcher.
02:10So let's first import it.
02:12We're going to import Flash events dot event dispatcher.
02:20So we have the event dispatcher.
02:22Now that we've imported our event dispatcher, we want to make sure that our timeout extends the event dispatcher.
02:29So now that we have our timeout and it extends the event dispatcher, we could start dispatching events.
02:35Now let's see how that works.
02:38Let's start by doing the other way around.
02:40Let's start by listening to those events.
02:42So if we go back to our application, and this time around, let me just really toss this aside.
02:50And I'm just going to really throw it out of our area because it's just distracting.
03:03Throw it all the way down there.
03:05It's not relevant.
03:07So what I want to do next is I actually want to add an event listener.
03:11As we've done before, when we add events, such as when we added enter frame event,
03:18where we've added any of those events, we called our object, and we asked for add event listener,
03:24and we told it what type, but we want to add our own events.
03:28So let's add some of our own events.
03:30So we're going to revisit our add event listener in a second.
03:33Now that we know just a reminder of how we connected to it.
03:37So we know how we add the events.
03:39When we're saying add event listener, what we're basically saying is add me to the list,
03:45or you could say an array, it's quite similar to an array,
03:48add me to the array of the things that are listening to the specific event.
03:53To create an event, all you really have to do in Flash, you don't have to create an array
03:58and start adding items into that array and then looping through that array and calling back those functions.
04:04All that functionality is done automatically for you as long as you're extending the event dispatcher.
04:10And all you have left is to figure out where do you want to dispatch the event.
04:13So for example, I always want to dispatch the event of update.
04:17In the event of update, I want to dispatch dispatch update.
04:21But I want to dispatch two different events here.
04:23I want to dispatch if our time is over, I want to dispatch one event.
04:27If our time is not over, I want to dispatch another.
04:30So I'm going to grab my if statement and just create an else statement.
04:34So in my else statement, first of all, this will be called each time as long as there's time left.
04:39Excellent.
04:40So to do that, all I have to do right now is type dispatch event, open a round bracket.
04:48And now what I have left is to actually send it an event object.
04:52And to do that, what I'm going to do right now is let me open and close my brackets
04:56and make sure that I've done that.
04:57And I'm going to go back into my bracket.
04:59I'm going to type new event.
05:02I'm going to open a bracket and close a bracket.
05:06But before, and I'm just going to go back in there and just open it again
05:09so we could see our hints.
05:12And in our hints, we could see that it's expecting a type as a first item.
05:16And all the rest are not mandatory.
05:18And we're going to revisit those as we look more in depth into working with events.
05:24All right.
05:25So all it is expecting is some sort of identifier to that event
05:29to let us give it some sort of a name.
05:31Now, I'm using the new event.
05:33And I have to make sure that I'm importing that new event.
05:35And you can see that in my import lines, automatically flash CS5 imported
05:39the flash.events.event.
05:42So the next step is we want to give a name to our event.
05:52And we could just send a string, any string, such as change.
05:55Because we really want to, we want to, we're just going to call it change.
05:58And all we're really trying to do is giving our event that we're dispatching a name.
06:02So then when we're listening to an event, we could then listen to an event
06:05that has a name like we were doing so far.
06:08Now, I could have wrote a string directly.
06:10But instead of that, there's a nicer way of doing it.
06:12And basically, we're going to approach our event class itself directly.
06:16And in the event class, there's constants.
06:18And we'll talk about what constants are in the future.
06:21But as the name implies, a constant holds a value that will never change.
06:25And funny to say that when we're picking the word change out of it.
06:30And all we really did here is we basically, it's the exact same thing
06:33as just typing directly a string value change.
06:37And the difference is, is that we're making sure that we're avoiding typos.
06:41And it just helps things look cleaner.
06:44And that's all we really have to do in our event dispatching.
06:48The next step is in our, when we're done with our timeout,
06:51is going actually to our app itself and listening to that event.
06:55So let's see how that's done.
06:57So we're back into our to.addEventListener, right?
07:01We could uncomment that.
07:03And this time around, we're going to do the exact same thing.
07:05We're going to listen to that event.
07:07And because we don't have it, we haven't imported it yet.
07:11So we're going to have to import it first, events.event.
07:18And in our addEventListener, we could just add another event listener saying,
07:22we want to listen to the event.change.
07:27But our second parameter is our listener.
07:29So I'm going to call it on time changed.
07:33On time or change, let's call it.
07:35So on time changed, we have to create that function.
07:41And now that we know what public and private is,
07:43we know that this is going to be a very private function.
07:46No one else is going to access it besides this class.
07:49And it's going to get one parameter, which is going to be the event type.
07:54It does not return anything.
07:57And let's just test out to see if we're actually getting crawled.
08:00So, hello.
08:03So first of all, we're listening to the event change on time or change.
08:07If we go back to our timeout, that's just before,
08:10let's just give you a little trace to see, is this area being called?
08:15I'm just going to put here just something random, trace1.
08:18I just want to see that that trace is being called.
08:23All right, so I'm already seeing that I have it, that's my problem.
08:26My trace is not being called anymore.
08:28And why is it not being called is because on line 11,
08:31our timer has been reset to zero, thus it's never being called.
08:35So let's set it to 10 again.
08:37And this time around, we should be getting one hello, one hello once a second.
08:42And it should be doing that for 10 seconds, but we could stop that at this point.
08:47The next step, let's just get rid of those extra traces that we don't need.
08:52And we're going to save that.
08:54And if we go into our...

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!