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

Hidden feature addFrameScript

A hidden feature since flash CS3 - addFrameScript. We will learn how to work with this hidden feature and see it in action. It enables us to create frame based animations. we will also figure out its limitations and capabilities.

Video Transcript

00:02Alrighty, so there's a few ways to do this.
00:04The easiest way to do it, and I want to show you just a hint of one way that you could
00:10stop it.
00:11One probable way is to go into your cup and just add in that last frame, call it to one
00:17of the internal functions like terminate or whatever you want to call it, or stop, don't
00:21refill, whatever function you want to call it.
00:24I'm going to do something else which is an undocumented feature in Actions Group 3 since
00:28Flash CS3.
00:29We're at Flash CS6 now, so it's actually widely supported and widely undocumented, and
00:36this is how you could do it.
00:38You could go to your cup of water, inside of our cup of water I'm going to add in the
00:42constructor a new line of code, and this line of code is called, it's a function called
00:46add frame script.
00:48So I'm going to go to add frame script, and this is only, only movie clips have that,
00:56and only extensions of movie clips have an add frame script.
01:00And what I could do is I could go to the current, not current frame, but total frames,
01:05and say when the total frames reaches, and you could put it in any number, any frame
01:09you want.
01:10So I'm going to put there in that total frame, when we reach that total frame, I want
01:14to call a specific function, we'll be, what will we call it, we'll call it stop cup,
01:23stop water, stop water, yeah that works for me.
01:27Alright so add frame script, the frame number you want to be called, and then stop water.
01:33So I'm just going to add that function all the way in the bottom, we'll make it a private
01:38function, and again if you don't remember why we do private and public functions, the
01:44idea behind private and public is very very simple, there's no reason for anyone to
01:48know about this function, no one should ever interact with it besides this function
01:52that we're sending it out to the add script frame, there's no reason for any other
01:56class to interact with it, and as such we're making it private.
02:00Contrary to that, the touch, deliberately we're making it public because we really
02:04want to control it out of the class, right now we're doing it only internally, just
02:08so we could build things up, but actually in reality, a lot of times when you're
02:11doing animations or control over a complete object, you want something that is greater
02:20than it to control it, because if every element controls itself, you get out of
02:25control very quickly, but if we have your main class that is controlling all the
02:29different animations, controlling the flow, but doesn't have any insight of what's
02:33actually going inside, you're going to create an application that's going to be a lot
02:36more sophisticated and easier to build, alright? So in our stop water, I'm going
02:41to immediately stop our water, and not only am I going to stop my water, I want
02:46to auto clear my timeout. So I'm going to create a new timeout, and let me just
02:53check, even though it doesn't really matter, because even if we call stop
02:56again, it's not the end of the world, so I'm fine with that. The only thing
03:01that's left really to test out is to see if this actually works. Now, we don't
03:05need to change any other logic, and the reason is, in theory at least, the reason
03:09is, is because if we reach frame 40, and we're in that frame right now, this
03:15should never come true anymore once we reach that element. So if I run my
03:19application now one more time, and I click onto the drink, the water is going to
03:23start emptying each time I click on it, and at some point when I reach the
03:27bottom of the cup, hopefully now when I click again, it's not going to roll
03:31again, but it does. So we know that we have here some sort of an issue that we
03:35were going to have to solve, so let's figure it out. So let me first put here
03:39a trace to try to see if our add frame script is actually getting called, so
03:44I'm going to call here, am I getting called? I'm going to trace your OTO before
03:51our touch itself, I'm going to trace OTO the current frame and the total frames
03:55to see if we're actually there or not. So let's give it a run. So the first time
04:01when we click on it, we have 41 frames where it won't frame 1, then 8, then 15,
04:07then 22, then 29, then 36, then 2. Interesting. So let's look at this for
04:16a second, let's see here what's going on. And one of the stuff that could have
04:21been, and by the way, did we ever get that trace that we hope to get? Let's
04:26look at our output again. Did we get a trace that told us that we reached that
04:30frame? We have not. So we definitely have here some sort of an issue, although
04:40that's a typo, that's my cats fighting in the background, and I'll fix that
04:47typo, although it would have nothing to do with it, and I'll just add here this
04:50just to see if that would help, and I'll run it one more time. And what's really
05:02interesting is that we're not getting it, it's not even popping up even once. So
05:07let's give this a try and just put it at frame 10 for a second and just see if
05:10it actually reaches frame 10 and if we actually get that I'm getting called.
05:15So when we have it on frame 10, definitely work, we got it running. But
05:20for some reason when we're calling the immediate last last frame, it seems
05:24like it's not working. So let's try and take our total frames and just subtract
05:28one out of it, because really the reality of it is, is that it doesn't really
05:33matter to us if it's called M40 or 41 right now because we have that extra
05:38frame. And again, this is an undocumented feature and that's probably one of the
05:41good things that I'm showing you also me making mistakes and how how I deal
05:44with them. And it's completely fine to make mistakes and you know a lot of
05:48times videos are way too polished in a level where you feel like people don't
05:52make mistakes. 90% of development is mistakes. What separates good
05:57developers and bad developers is when you make a mistake you just move on and you
06:01test it and you explore it until you figure it out and you take it in
06:04different avenues. But I think it's important for you guys to see that I'm
06:08making these mistakes so you guys can see how I deal with the mistakes and
06:12how I try to figure out a solution to solve it. So I had no idea why it
06:15wasn't working, I had no idea and I don't even know now if it's going to
06:19work but let's check to see if we're going to get that trace now. And again you
06:22can see how many times I use traces so again I'm going to click over and it
06:26should have frame 40 let us know that it got there and it definitely did. Now
06:30our animations won't work still maybe but it actually is because now it's
06:34getting it's it's triggering at line 41 and now I know exactly what's going
06:39on. For some reason it takes it one extra frame before it completes the
06:45stop. So basically when stop water is called it is called and if it's called
06:49on frame 40 it will actually only affect frame 41. It seems like maybe
06:57because it's an undocumented feature which basically means that it could be
07:00that it's going to change. But it looks like when we're actually calling the
07:03frame and even though we were expecting it to be in frame 10 let's see if my
07:07theory is right. Let's see what is our current frame when we're being
07:11called and even if even if we see their 10 that does not mean that I'm wrong
07:16and and I'm not wrong because we could see that now it's going to 41 even
07:20though we're asking for it to go to one before 41 and that just means that
07:25there's some something wrong with this interface something's wrong with this
07:27ad frame script and and again and this really comes to show you that it's okay
07:33even the developers of Adobe made a mistake probably or we're not using it
07:39the way up or we're not using the frame the way they wanted them to they wanted
07:43a suit but then again it's not a documented feature so it doesn't really
07:46matter. With that said we're very much in the cutting edge now if you don't feel
07:52comfortable being in the cutting edge you could always add a backup plan such
07:56as just calling directly the stop water but if you do that you would have to
08:00make this function public but then you could just go directly into that last
08:04frame and make sure that it's being called and I don't want to go too deep
08:09into this but when you think about it when a frame is rendered there's a few
08:13stages to the rendering of a frame there's the before it's rendered there's
08:17after it's rendered right so what what very much could be is that our call is
08:23happening exactly in that millisecond before it's moving on to the last frame
08:28when we call the stop it's just too late already we're already in the next
08:31frame because the stop really should have come before the rendering and not
08:36after the rendering that's what my my my guess is so there we go so we have
08:40your cup of water we have two two animals and we have fire which we're not
08:44gonna touch but I'm leaving that to you to create some sort of a fire effect so
08:48we have a cat we have a dog and we have water and really our goal right now is
08:52to go into our main class and remove those click events from our elements
08:57and move the click event externally into the main and make it work there and
09:00that's gonna be our step that will help us try to figure out how could
09:04we treat and how could we talk both a cat and a dog and a cup of water in the
09:10same language using the same code

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!