如何避免凌乱AS3 code凌乱、code

2023-09-08 14:03:59 作者:拥你最暖

我一直在编程使用ActionScript 3的一小会儿,我已经注意到,我的code中的自然进步似乎采取一个巨大的文档类的形式与几十个成员变量,回调和手柄在舞台上的物体。简而言之:这是怎样的一个烂摊子

I've been programming with ActionScript 3 for a little while and I've noticed that the natural progression of my code seems to take the form of one giant document class with dozens of member variables, callbacks, and handles to objects on the stage. In short: it's kind of a mess!

事情是,我实在看不出办法解决它(尚未至少)。我用不同的关键帧在时间轴上重新present不同的状态在一个应用程序,虽然我有时间轴上的一些code权(如鼠标点击一个影片剪辑快速的东西),大部分的逻辑刚刚结束了倾倒在主文档类。

Thing is, I don't really see a way around it (not yet at least). I use different keyframes on the timeline to represent different states in an app, and while I have some code right on the timeline (for quick things like a mouse click on a movie clip), most of the logic just ends up dumped in the main document class.

所以,我不知道......有什么好的方法来帮助neaten了这个code-了野生?或者,这是正常的吗?我来自一个C ++背景,而我喜欢写面向对象的东西,但我看不到的方式来延续那种结构的闪存。任何有识之士将是非常美联社preciated。

So, I'm wondering... What are some good ways to help neaten up this code-gone-wild? Or is this normal? I come from a C++ background, and I like writing object-oriented stuff, but I can't see a way to carry over that kind of structure to Flash. Any insight would be really appreciated.

感谢

推荐答案

您可以将您的许多C ++的技能,你的AS3项目。

You can apply many of your C++ skills to your AS3 project.

有很多的技巧。很高兴你提出了把code中的主时间轴上。而不是把code在时间轴上的(这在AS2方案,并与AS3可以完全避免它太常见的),我建议每个对象的思想作为一个单独的类。你的视频剪辑你申请点击鼠标code,例如,可以有自己的类创建的对象。说在MovieClip是一个球的图形。你应该创建一个扩展(继承)MovieClip类一球类,并在其中处理鼠标点击事件:

There are lots of tricks. Glad you brought up putting code on the main timeline. Instead of putting code on the timeline (this is way too common in AS2 programs and with AS3 you can completely avoid it) I would recommend thinking of each object as a separate class. Your movieclip that you are applying mouseclick code to, for example, could be an object created with its own class. Say the MovieClip is a graphic of a ball. You should be creating a 'Ball' class that extends (inherits) MovieClip class, and handle the mouseclick event within it:

package 
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class myObjects.Ball extends MovieClip 
    {
        public function Ball ()
        {
            this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        private function mouseDownHandler(event:MouseEvent):void
        {
            // Code
        }
    }
}

然后,找到你的球在库面板中的影片剪辑中,右键单击它,属性,切换到高级模式,检查了出口的AS。现在,请注意你的影片剪辑已经引用MovieClip类的基类?你不会需要这个任何更多,因为你的球类扩展MovieClip类..所以在类字段写,myObjects.Ball并清除基类领域。你应该可以看到一个绿色的对勾,如果你写了一个路径,闪存IDE可以找到您的命名空间中的球类。

Then, find your MovieClip of the ball in the Library pane, right click it, Properties, switch to Advanced mode, check off Export for AS. Now, notice how your MovieClip already references the MovieClip class as its Base Class? You won't need this any more since your Ball class extends the MovieClip class.. so in Class field write, 'myObjects.Ball' and clear the Base class field. You should see a green checkmark if you wrote a path to your namespaced Ball class that the Flash IDE can locate.

现在你的球类将使用该MovieClip所以当你在你的主类创建球的一个新实例,你可以使用它像一个影片剪辑并将其附加到舞台上,动态。或者,你可以只通过手动拖动球影片剪辑上有添加时间轴。

Now your Ball class will use that MovieClip so when you create a new instance of Ball in your main class, you can work with it like a MovieClip and attach it to the Stage, dynamically. Or, you could just add it in the timeline by manually dragging the Ball MovieClip on there.

扩展类我解释是AS3版的传承的(球类继承MovieClip类)。您也可以使用其他的面向对象的概念,如,多态性和封装。你应该封装你code到单独的类只要有可能。说,如果你在你的项目中有几个不同类型的球影片剪辑的,并且要球类是一个父类足球,池球,和棒球。每个这些子类可以扩展Ball类。我发现有用的大型项目的另一件事是创建一个静态类来处理我所有的应用程序的事件。因为我把它定义为公共静态类我可以给我的每个类和它的变量只能用于应用程序的持续时间创建一次导入。这是非常有用的。

Extending a class I explained is AS3's version of 'Inheritence' (Ball class inherits the MovieClip class). You can also use other OO-concepts like, polymorphism and encapsulation. You should encapsulate your code in to separate classes wherever possible. Say if you had a few different types of ball MovieClips in your project, and you want Ball class to be a parent class to Soccer ball, Pool ball, and Baseball. Each of those child classes could extend Ball class. Another thing I have found useful for large projects is to create a static class to handle all of my application's events. Since I define it as a public static class I can import it in to each of my classes and its variables are only created once for the duration of the application. This can be extremely useful.

我也创建了自己的伪析构函数的类在努力与AS3的工作更象C ++。拉这一关最简单的方法就是你摧毁一个对象的实例之前调用伪析构函数。我没有让它在一个应用程序中自动发生,因此,如果对此感兴趣的人,我可以追查code ..但AS3在后台处理垃圾收集,通常析构函数是不需要的,但也许我只是觉得它不是'牛逼必要的,因为我开发的坏习惯在AS3节目太久了。

I have also created my own pseudo-destructor in classes in an effort to work with AS3 more like c++. The easiest way to pull this off is to call the pseudo-destructor before you destroy the instance of an object. I did make it happen automatically in one application, so if this interests anyone I can track down the code.. but AS3 handles garbage collection behind the scenes and usually a destructor is not needed, but maybe I just think it isn't needed because I developed bad habits out of programming in AS3 for too long.

我个人认为,你越努力开发应用程序在AS3中,如果您正在开发的C ++中,更多的乐趣它会获得更多的可重复使用的code变。保持很快代替$ C $的搞砸了。C,你将有动作的文件乱七八糟的笑..一个双刃剑位,但whatevs。

Personally, I think the more you strive to develop apps in AS3 as if you are developing in C++, the more fun it gets and the more reusable your code becomes. Keep it up.. soon instead of a mess of code you will have a mess of actionscript files lol.. bit of a double-edged sword but whatevs.