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

Integrating Intervals

Creating our cat class. Our cat class is inherited from the Animal class. In this sample we will create a small animation with more user interactive features. Our cat does not like getting pet each time we pet our cat he will get more and more aggravated.

Video Transcript

00:00So I want to move on to our cat,
00:02and our cat is going to be a little bit more tricky.
00:04What we want to do with our cat again
00:06is we want to make sure that if you click on our cat
00:10more than three times, then our cat is going to get annoyed.
00:14Now, if we just do that,
00:15then our application is going to be completely done.
00:17So we'll add another layer of time onto it.
00:20And the idea itself is if you supposedly pet a cat
00:23and then wait for a second and pet it again,
00:25then it's not the end of the world.
00:27But if you go to a cat and you're like poking him
00:29then he's going to get all agitated and annoyed at you.
00:32So the idea itself is basically to add time into it
00:35to give it a little bit more of depth
00:37and a little bit more coolness to it.
00:40So I'm going to create a new private variable
00:42and I'm going to call this touch count.
00:46It's going to be an integer.
00:48Originally it's going to be equal to zero.
00:50And really all I really want to do
00:52is whenever my cat is touched,
00:54we want to add one to our element, to our touch count.
00:58Now, if our touch count is greater than three,
01:02then we want to go to and stop.
01:08We want to go to and stop to our,
01:12what was the name of that frame?
01:14Let's see, I think it was called stressed tense.
01:17All right, so tense works.
01:20And probably let's not put an underscore in this one
01:22if we're not putting an underscore in the other.
01:24So it's going to be relaxed and tense.
01:30So we're going to go into our,
01:32we're going to go to and stop to our frame tense.
01:37Run our application.
01:38If we click one, two, three, four,
01:42our cat is going to get annoyed
01:43because we started from zero,
01:45we added one to greater than three.
01:48So we want it greater or equal to three,
01:50then it's going to take us to tense.
01:52One, two, three, cat is tense.
01:55Now, we want to add one more thing is basically
01:57as long as our touch count is greater than zero,
02:02we want it to continuously basically get less
02:06and less severe every second, let's say.
02:10So to do that, we're going to make sure
02:11that every time we start our,
02:14if touch, we're going to, first of all,
02:16we're going to create another private variable,
02:18which is we're going to store inside of our interval ID.
02:23It's going to be a type of integer.
02:25And we want to make sure that, first of all,
02:28we're going to import our helper,
02:32our helper set interval and clear interval.
02:35But actually, we're going to use, should we do that?
02:39Yeah, set interval, clear interval.
02:41That works for us.
02:42Import, flash, utils, set interval,
02:47import, flash, utils, clear interval.
02:55All right, so first thing we always want to do
02:57on this, something we talked about
02:58when we were working on the time videos
03:01is always clear an interval even before you start using it.
03:04The reason is because we want to make sure
03:07that we never start up a new interval
03:10while another interval is working.
03:14So we're going to set a new interval
03:15and in our set new interval,
03:17we're going to call the function get10s.
03:24And we're going to call it every, unwind actually.
03:30Unwind is a better name
03:32because every second we're going to call it
03:36and we're going to create a private function, unwind.
03:43And unwind, once a second,
03:44will get less and less winded up.
03:46So we first want to make sure that as long
03:50as our touch count is greater than zero,
03:55then we're going to do this.
03:57If not, we are going to completely stop it.
04:02So we're going to clear interval
04:06and we're going to clear our interval.
04:09If we're not clearing our interval,
04:10what we want to do is we want to subtract one
04:13from our touch count.
04:15If our touch count is less than three,
04:21then we want to go to end stop.
04:25And the name of that frame was, I always forget it, relaxed.
04:29So we're going to take relaxed, don't do it.
04:33When you got it, do it.
04:35Okay, so you're not paying me to sing
04:38so I'm going to stop singing.
04:39I'm kidding, it's like, you don't want me to sing.
04:42Okay, so we got here.
04:43So this seems just about right.
04:45Let's just make sure that the logic is working.
04:47So every time we touch, basically,
04:48that's every time we click,
04:50we're going to clear our interval
04:51and start our interval all over.
04:53If our touch count is greater or equal to three,
04:55then we're going to get all tens.
04:58And once a second, we're going to unwind.
05:00And how do we unwind?
05:02If our touch count is greater than zero,
05:06we will deduce one out of our touch count.
05:10If our touch count is less than three,
05:13then we are going to relax.
05:15And otherwise, if our set interval is still going on
05:18and we're at zero or below, we will clear our interval.
05:22So let's run our application
05:23and let's see if our cat now
05:25is behaving the way we want it to.
05:26So one, two, three is going to make our cat tens
05:29and a second passes and our cat relaxes.
05:32One, two, three, four, five, six, seven.
05:34So it's going to take that seven seconds.
05:36This is going to take three seconds.
05:37This is going to take four seconds.
05:39And it definitely worked.
05:40And it definitely did exactly what we wanted it to do.
05:43So basically every time we click on it,
05:45we're prolonging the amount of time
05:47that our cat is going to be annoyed at us.
05:50And whenever that timeframe passes
05:52and he has enough time to relax,
05:54then our cat gets all cuddly and happy again
05:57and non-aggressive.
06:00So there we go.
06:01So we created our last kind of more visual example
06:06of working with inheritance.
06:08And we're ready to move on to another element
06:11that will not be part of our inheritance line.
06:13And let's do that in the next video.

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!