views:

155

answers:

2

I'm aware that ActionScript 3.0 is designed from the ground up to be a largely object-oriented language and using it means less or even no timeline code in Flash documents.

I'm quite experienced with OOP and am comfortable writing classes. However, since I mostly use Flash for animations, I hardly ever need to write ActionScript code other than for preloaders, subtitles, quality controls, website links and so on. In fact, I still set my Flash movies to use AS2 to this day because I'm used to gotoAndPlay()/gotoAndStop(), AS2 preloaders, subtitles, quality controls and even getURL(). Of course, I really want to move on now that practically everyone's on Flash Player 9 or 10 and now that I've dabbled with other OO languages like Java, C# and Objective-C too.

I'm a complete newcomer to AS3 and am not very learned with AS2 either. Considering my current use of ActionScript, are there any cases where it's still OK to use very simple AS3 code in the timeline instead of moving code to a class, especially since moving to a class might mean unnecessarily increasing the number of LOC from 4 to 40? (Heck, is the latter case ('instead of ...') even a valid concern at all?)

+1  A: 

If you are already familiar with AS2 (and you have experience with other languages as well), switching to AS3 shouldn't be too hard. There are a few places where things changed quite a bit: basically the event model and some widely used apis, like flash.net (i.e., loading stuff, be it images, swfs, sounds, xml, etc) and the display list (much more options than just movieclip, more coherent, reparenting, etc). Working with xml changed (for good) too and it's more powerful and easier (but you can use the old api if you feel like, it's still there). The bulk of language itself hasn't changed much, though.

I'm sure you are aware of the potential problems of having code in the timeline. Well, the same applies to AS3, though some things like placing code on objects is not allowed in AS3.

I don't see a problem in placing stuff in the stage in the IDE and using movieclips for animations. IMO, that's why they're there in the first place. I know some people obsess about creating and positioning everything by code. I'm not one of those. For me, the IDE could be a good tool for layout, tweening, etc, at least in most cases (for some very dynamic stuff, on the other hand, you'd need to code the layout too).

So, I wouln't not outright advise against moderate use of timeline code. If it suits your needs, fits in your workflow and lets you do what you need to do without being an impossible to maintain (or even follow) mess, why not?

If at some point you need to put some stuff in classes, you refactor and move your code as you go.

Juan Pablo Califano
Thanks for your advice. Yes, I'm not one of those with anything against the IDE either - though once I do serious AS3 I will certainly code using FlashDevelop or something.
BoltClock
No problem. I use the IDE regularly, but for preparing assets and maybe adding a stop() here and there or a dispatchEvent to tell the code some animation started or finished. But that aside, I write code in FlexBuilder. I used FD for a year or so and I think it's really great (as an editor, I think it's better than FB). I switched to FB because of the debbuger and profiler.
Juan Pablo Califano
+1  A: 

From the examples you've given on how you use Flash, nothing has really changed to make any of that more difficult. There is no reason why you should use external code for all the above. You can work pretty much the same as before (gotoAndPlay, gotoAndStop still exist and work just fine). The only thing that comes to mind that is more tedious than it used to be is the fact there is no more getURL(). However, Senocular has remade the getURL class and you can find it here.

I would say, only if your project is particularly code heavy, then move most if not everything into classes and structure it properly in an OOP manner. Heck, you can even just pass a reference to your stage to a base class and just operate pretty simply in that manner as well.

In your .fla timeline:

import com.yourdomain.Main;

var main:Main = new Main(this);

In your external Main Class:

package com.yourdomain
{   
    public class Main
    {
        private var mainTimeline:Object;

        public function Main(_mainTimeline:Object):void
        {
            mainTimeline = _mainTimeline;

            mainTimeline.gotoAndPlay("fScream");
        }
    }
}
Daniel Carvalho
Thanks for the example code. Also, do I add an `ENTER_FRAME` event listener to the `Main` class and check for certain frames in its handler if, for example, I want to manipulate the timeline in later parts of my movie?
BoltClock
You'd have to modify the Main class, like make it extend Sprite in order for it to receive events. But even without that, if you had to trace out mainTimeline.currentFrame, it will return you the current frame your main timeline is on, without the use of listeners.
Daniel Carvalho