View Full Version: AS2 Crash Course!

lassie >>Discussions >>AS2 Crash Course!


<< Prev | Next >>

bigmac- 01-08-2008

Chris, You're never clearing the interval. You took my example I see and built upon it with checking for and clearing any existing interval before setting the new one. So, now build upon that idea. The first thing you should do within the show_text() function block is clear that interval so that it only fires once. So, update the function to look like: function show_text() { clearInterval(intervalId); main.title_brief.brief_text.gotoAndPlay(2); } That should do it. However, now that we've turned that into a working solution let me also mention that there is something else that would be more effective to use here. Look up the "setTimeout()" command. setTimeout works almost exactly like setInterval() except that it is specifically designed to only fire once. So, using a setTimeout() would do the same thing as setInterval() except with less code, complexity, and garbage collection (the process of cleaning up code that is no longer needed). *** Also, here's an FYI thing that I might as well offer: syntax. Technically there is no wrong way to write code, but standard practices do exist for good reason. I notice that you're using underscores for everything, which in practice tends to be a poor choice. Camel Case is preferred for method names: showText(); textAppear(); As for instance names, underscores are generally reserved as an information delimiter. For example, you generally see instance names cited as: myMovie_mc myButton_btn myTextField_txt myWeirdThing_mc The above names give you some frame of reference about the objects they reference... the prefix acts as the identifier and the suffix tells us its object type. While Flash doesn't care about this naming convention, it's helpful for us as programmers. Case of point, can you venture a guess as to what type of object the weird thing is? :D Also, in more advanced programming you can start embedding object parameters into its instance name. Take the Lassie action selector buttons for example, which are called: action_0 action_1 action_2 The numeric suffixes aren't by coincidence. When we break those names into chunks of data (separated by the underscore), the data tells the button what action to call.

fatbuoy1- 01-08-2008

Ahhh... yes id remembered you saying it was important to clear the interval, but I thought because I had clearInterval BEFORE I set the interval evey time it would work... but I now see thats complete nonsense! Also, thanks for the syntax advice, ill definately take it on board for the future, I can see how it could be useful. Have you any idea why the last part, the _currentframe bit, isnt working? I need it so that the if the user revisits a section, the information wont 'appear' again, when its already there. By my reckoning if (_root.main.title_brief.brief_text._currentframe != 5) { text_appear() } Should mean "If the movie brief_text is NOT currently at frame 5, then call the text_appear() function" Thanks again! Hope this isnt getting annoying for you..

fatbuoy1- 01-08-2008

Oh and the weird things a movie clip? Possibly of a dog with six tails and no nose?

bigmac- 01-08-2008

Yes, the weird thing would probably be a MovieClip. :D As for the frame checker... this is a pretty tough question to answer out of context. This isn't really a code question as much as it is an architecture question. Without seeing your architecture, it's difficult to guess at. Either way, this looks very convoluted (no offense). I'd say the best place to start would be some good old fashion debugging. My method of choice is trace(). If you haven't gotten to know the trace statement yet... well, then I don't know how you've been able to do ANYTHING within Flash!! I'd be lost without it. trace(value) just outputs the specified value in the output window so that you can see what is being referenced. It's the god of all debugging. So, I'd start by tracing out the value of the currentframe lookup to see –for certian– what flash is hitting at that point. It might be very informative if the value is not what you're expecting it to be.

bigmac- 01-08-2008

And no, you're not annoying. This is scary though... I've been an AS3 convert for all of three weeks now and this stuff is starting to look like ancient history. Yikes!

fatbuoy1- 01-08-2008

Id best get all my questions in quick then before you forget it all! And I KINDA know how you feel, wasn't much more than three weeks ago all I knew was how to use the stop() and gotoAndPlay() function, and even then I just double clicked them in the menu! :D

bigmac- 01-08-2008

What version of Flash are you using, BTW? Are you confined to AS2 or just choosing it out of habit or by choice of a teacher who hasn't made the switch yet? I've got to say: AS3 is beautifully simple in some ways. The simple stuff IS (for the most part) simpler in AS3.

fatbuoy1- 01-08-2008

CS3, so AS3 is an option. No my tutors don't know it as far as I know. I kind of shied away from CS3 basically because I couldn't find gotoAndPlay() or stop() in the menu... (embaressed silence)

fatbuoy1- 01-08-2008

that trace thing worked a treat! i was a frame out and hadnt noticed. Thanks!

bigmac- 01-08-2008

So you have been developing all this time without the trace() statement in your arsenal?!? That's truly impressive. Kudos for not having given up in frustration!! I'd go loony without being able to keep my finger on the pulse of the app. Concerning AS3 – if you have the capacity to use it, then DON'T hesitate. Finish this project off in AS2, then don't start anything else with it. You're in a unique position to learn Flash programming starting with AS3... and while that'll be a challenge, it'll be easier and far less confusing than trying to migrate your knowledge over to AS3 from AS2. Believe me, it's a pain in the butt. While most things are similar, they're JUST different enough that your understanding is moot. It's incredibly frustrating. Case of point on this: today's discussion about setInterval() and setTimeout() is totally outdated. Both functions have been removed from AS3 and replaced by a marriage of the two which is a million times better: the Timer class. So, you've already acquired antiquated knowledge that you should purge forever and move on from. Amazing the rate at which knowledge gets outdated, isn't it?

bigmac- 01-08-2008

incidentally, using an AS3 Timer, you'd do this... // import timer assets for use import flash.utils.Timer; import flash.events.TimerEvent; // create new timer var timer = new Timer(delayTime, repeatCount); // add event listener to call when timer finishes timer.addEventListener(TimerEvent.TIMER_COMPLETE, responderFunctionToCall); //start timer timer.start(); What's really cool about it is that it's metaphorically identical to holding and clicking buttons on a stop watch. Once you create the timer object, you can start it, stop it, reset it, etc... While it looks like a lot of code compared to setInterval() / clearInterval(), it's actually substantially less. Remember all the conditional logic crap we had to compose to keep a leash on our intervals, lest they leak into memory oblivion? All that garbage is gone. So while it takes a couple extra lines of code to set up a timer, once its configured you can just leave it alone to do its job rather than write tons of conditional logic all over the place that babysits it.

fatbuoy1- 01-08-2008

Probably a good job I don't really understand setInterval() then isn't it?! :D Yeah it is quite scary, still im sure theres good points too :D Whats the main benefits of AS3? Is there any compatibility issues at the moment?

fatbuoy1- 01-08-2008

Cool, so im guessing theres a special value for repeatCount to make it happen once? Whats the event listener?

bigmac- 01-08-2008

Cool, so im guessing theres a special value for repeatCount to make it happen once? Whats the event listener? Ha... it's pretty simple: // repeat count of 0: calls iterations indefinitely like setInterval() var timer = new Timer(500, 0); // repeat count of 1: calls one iteration, like setTimeout() var timer = new Timer(500, 1); // repeat count of X: calls X iterations, which you would previously have to do with setInterval coupled with custom cycle-counter logic var timer = new Timer(500, X); As for event listeners... they are the key to everything in AS3. They're pretty cool too, but you have to understand their difference from AS2: AS2 uses a call model. Think of that as using a telephone. You call a specific number and get a response. However, if the number is disconnected then you get an error. AS3 event listeners work like a radio broadcast: a radio station sends out a signal of their broadcast. They do this weather they have any listeners tuned in to their station or not. If no one it listening, who cares? No errors are encountered. So, in the case of the Timer object, that timer will broadcast an event when it completes. If a function is listening to the timer, then it gets called. If the timer had no listeners... well, no worries! It's pretty slick.

bigmac- 01-08-2008

PS: main benefit of AS3 is that AS2 is being depreciated by Adobe. Give it a couple of Flash versions and I doubt you'll even have the option to use AS2 anymore. My guess is that Flash Player 11 won't even natively support AS2 playback... it'll just run them using an older version of Flash Player. That means that whatever cool new features Adobe adds into Flash in the future will not be available to AS2 developers. It's also a handy benefit that AS3 is all-around better than AS2. The weaknesses of AS3 really aren't severe enough to undermine its strengths. Plus, we're still working with the first release of the language. It's got nowhere to go but up.

Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.