带有类文件的movieclip组件

时间:2012-05-10 03:49:06

标签: actionscript-3 class formatting

我需要建议我应该怎么做。我有一个大型应用程序,其中所有代码都在frame1的主时间轴上。我已经开始尝试将代码放在类文件中,并告诉某些movieclip组件将这些类文件用于其代码。到目前为止,我大部分都是成功的,但也许这不是我应该怎么做的。

主时间轴上有8个按钮,名为O1到O8。然后我在我的库中有一个movieclip组件,里面有它自己的一组O1到O8按钮。此组件已添加到其自己的层的阶段,并给出实例名称battleDashboard。当它是一大块代码(没有任何条款)时,我曾经访问过这样的按钮。

主界面主时间线上的O1 //按钮 battleDashboard.O1 //在battleDashboard组件内的按钮

自创建类以来,我一直使用“this.O1”访问组件内的buttos。使用BattleDashboard动画片段组件的属性面板链接类文件。它应该在战斗发生时被调用,然后返回主界面,完成时隐藏自己。

由于我在这个应用程序中拥有超过10,000行代码,我真的希望组件有自己独立的代码。我发现我必须重写很多函数并将它们包含在以前共享的类文件中。我还必须花时间使用“公共功能集”复制对象和变量,然后使用自定义事件检索它们。我最好把它全部放在主时间轴上,只是把外部作为文件?

1 个答案:

答案 0 :(得分:0)

这个问题无法回答,因为它...... 如果所有代码都不是基于OOP并且驻留在时间轴上,为什么要创建单独的类文件?请更具体一点。尝试从应用程序制作对象时遇到的问题是什么?

通常,您需要主.fla文件的document属性中的主类。 这个mainClass应该扩展MovieClip。 这是org / Main.as中的一个示例 在文件夹中创建一个空的Flash文件,然后在名为“org”的内部创建一个文件夹。 将此代码放在此文件夹中名为Main.as的文件中,并将org.main设置为文档类属性中的主类。 在动作面板中fla的第一帧:

var aVarOnFirstFrame:String = "some variable value";

此示例显示如何调用Flash影片的主容器以及您的类如何与doc交互。 我希望这会有所帮助。

您将看到如何调用变量和方法。

package org {
import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Main extends MovieClip{

    private var mainStageObject:DisplayObjectContainer;
    private var theStage:Stage;

    public function Main(){
        mainStageObject = this.parent;
        theStage = this.stage;
        // Let's call this :
        trace(this + " == \"[object Main]\"\nAND NOT \"[object MainTimeLine]\"");
        trace("which is the default if no main class is specified for the .fla root Document")
        // this.stage is a shortcut to the top level element
        trace(this + ", paced on " + this.parent + " == " + this.stage + " == " + theStage);
        trace("comparaison for the three calls : ");
        var equalityParentStage:Boolean = (this.parent === this.stage);
        var equalityStageAndTheStage:Boolean = (this.stage === theStage);
        var equalityParentAndTheStage:Boolean = (this.parent === theStage);
        var equalityDisplayOContainer:Boolean = (this.parent === mainStageObject);
        var equalityForAllThreeProps:Boolean = ((this.parent === this.stage) && (this.stage === theStage) && (this.parent === mainStageObject));
        trace("this.parent === this.stage ? " + equalityParentStage);
        trace("this.stage === theStage ? " + equalityStageAndTheStage);
        trace("this.parent === theStage ? " + equalityParentAndTheStage);
        trace("this._parent ==== mainStageObject ? " + equalityDisplayOContainer);
        trace("this.parent === this.stage === theStage === mainStageObject ? " + equalityForAllThreeProps);
        this.addEventListener(Event.ADDED_TO_STAGE,onMainIsOnStage,false,0,false);
        this.addEventListener(Event.ACTIVATE,onMainIsActivated,false,0,false);
    }
    private function onMainIsOnStage(e:Event):void{
        trace(e.target.aVarOnFirstFrame);
        // -> outputs : null;
    }
    private function onMainIsActivated(e:Event):void{
        trace(e.target.aVarOnFirstFrame);
        // -> outputs : some variable value;
    }
}
}

/*
[object Main] == "[object Main]"
AND NOT "[object MainTimeLine]"
which is the default if no main class is specified for the .fla root Document
[object Main], paced on [object Stage] == [object Stage] == [object Stage]
comparaison for the three calls : 
this.parent === this.stage ? true
this.stage === theStage ? true
this.parent === theStage ? true
this._parent ==== mainStageObject ? true
this.parent === this.stage === theStage === mainStageObject ? true
null
some variable value
*/