开发者

Placing AS3 code on stage/MC timelines a la AS2 instead of in classes

开发者 https://www.devze.com 2023-01-02 05:24 出处:网络
I\'m aware that ActionScript 3.0 is designed from the ground up to be a large开发者_运维问答ly object-oriented language and using it means less or even no timeline code in Flash documents.

I'm aware that ActionScript 3.0 is designed from the ground up to be a large开发者_运维问答ly 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?)


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 the 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 wouldn't 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.


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");
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消