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

A Deeper Look At Events

Well we are very new to events so we aren't going to 'dig' to deep into it (we will save that pain in the a$$ to a longer more detailed course) but lets see one more goodies how we can reuse our function to be used on both buttons. It will be a bit of magic right now but don't worry in a few hours this will become a natural feature.

Video Transcript

00:00Now before we say goodbye to this topic,
00:03let's just look one more time at the events
00:05and add one more interesting feature about events.
00:08Because this function itself right now
00:11is actually calling exclusively to button one,
00:14it's not really reusable, is it?
00:17Because we can't say button two as well.
00:19We couldn't just grab this whole line of code
00:23and paste it right inside here
00:26and use button move two instead of button move one, right?
00:30It will work, it's not gonna do what I want it to do.
00:33If I click on it, it's gonna move the other button,
00:35but that's not really what I want.
00:36I want this button to move around when I click on it
00:39and I don't really wanna create a new function for that.
00:41Yeah, so you could create a new function,
00:43but why create a new function
00:44if you could use the same function for both?
00:46Let's see how we could do that.
00:48So I'm gonna comment down all this code.
00:54I'm just gonna paste it in again.
00:56And this time around, instead of approaching
00:58our button one and button two,
01:00I'm just gonna create a new variable here,
01:03which I am going to call SB for simple button.
01:08And it's gonna be of a type of simple button.
01:13And I'm gonna put the value to be E dot target.
01:18And the E dot target is gonna return to me
01:20whatever dispatched the event, our current target.
01:24And without going too much into this,
01:27because this could be like a whole topic
01:28for a whole hour or maybe even two,
01:30there are a few series of videos about target
01:32and current target and the differentiations between them.
01:35But for now, for our case and scenario,
01:38our target is gonna return to us
01:39our object that was clicked on.
01:42All right, and all I really have to do
01:43is just make sure so Flash won't generate an error,
01:46which it will right now if I run it,
01:48if I run the application now,
01:49it's gonna tell me that there's an explicit correction.
01:52We wanna fix that.
01:54So we wanna actually explicitly tell Flash
01:55because Flash by default thinks that the target is what?
01:58If we click on the target again,
01:59we'll see the target, it thinks it's an object
02:01because it could be anything, right?
02:03Any event, every target has different things.
02:05So sometimes it's visual, sometimes it's not.
02:08But we wanna actually tell that our target
02:10is a simple button.
02:11We know it's a simple button.
02:11We don't have to worry about it
02:12because we just know it is.
02:15The way we could set it is we could just have to,
02:18we could basically, or wrap it around
02:21and just say as simple button.
02:26Let's define our simple button,
02:28or let's help it out a bit.
02:30Basically by saying as simple button,
02:32we're basically saying take this.
02:34Now tell Flash it's a simple button so it won't bug us.
02:38All right, or if we wanted to,
02:39we could have not even said it's a simple button,
02:41but for our case, we wanted to do that
02:43because we wanna get the hints
02:44and it's kinda nice.
02:46It's good for our practice.
02:47So if we run this baby one more time,
02:49we shouldn't get any errors
02:51and we're not, now it's still not working, right?
02:53But we're getting closer.
02:54Now we could grab that SB
02:57and we could just paste it in any place
02:59where we're referencing directly the specific button.
03:03And in that way, we're basically changing all the actions
03:06to become actionable to whatever it was clicked on
03:10because we're approaching the target that was just clicked
03:13which we got from our event information.
03:16And now we basically leverage that one function,
03:19that one event, right?
03:22That one event listener
03:24to be able to be used on both buttons
03:26which in theory we could have done it
03:27for as many buttons as we wanted
03:29if we wanted all those buttons to have the same behavior.
03:32Now, usually we probably wouldn't wanna have
03:34the same click events,
03:35but a lot of times we would wanna have
03:37the same rollover or rollout
03:40which we didn't look into,
03:41but we will as we start working with movie clips
03:43because buttons really have already default rollovers, right?
03:50And here we go.
03:51So if I click on this element, it will move around
03:53and no matter which element I click on,
03:56it would move around.

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!