我怎么称呼我的。来自另一帧的类文件?

时间:2012-10-28 22:18:35

标签: actionscript-3 class

我在外部设置了代码。类文件此类文件允许使用样本等几个鼠标功能,例如颜色转换。现在代码运行良好但它似乎只在第1帧中工作。我想在加载as时将我的所有作品从第1帧移动到第4帧。我的功能在第4帧中附带的类文件。所以基本上我只是试图从第1帧到第4帧移动所有内容。这是怎么做的?我尝试发布设置并将eport classes选项更改为第4帧但没有成功。下面我的代码是外部的。文件。

package code
{
    /*****************************************
     * DrawingSample4 :
     * Demonstrates dynamic color transformations.
     * -------------------
     * See 4_color.fla
     ****************************************/

    import fl.events.SliderEvent;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    import flash.display.MovieClip;
    import flash.geom.ColorTransform;



    public class Drawing5 extends MovieClip
    {
        //*************************
        // Properties:

        public var activeSwatch:MovieClip;

        // Color offsets
        public var c1:Number = 0;   // R
        public var c2:Number = 51;  // G
        public var c3:Number = 51;  // B

        //*************************
        // Constructor:

        public function Drawing5()
        {


            // Respond to mouse events
            swatch1_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
            swatch2_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
            swatch3_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
            swatch4_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
            swatch5_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
            swatch6_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
            apply_btn.addEventListener(MouseEvent.CLICK,clickHandler,false,0,true);
            previewBox_btn.addEventListener(MouseEvent.MOUSE_DOWN,dragPressHandler);

            // Respond to drag events
            red_slider.addEventListener(SliderEvent.THUMB_DRAG,sliderHandler);
            red_slider.addEventListener(Event.CHANGE,sliderHandler);
            green_slider.addEventListener(SliderEvent.THUMB_DRAG,sliderHandler);
            green_slider.addEventListener(Event.CHANGE,sliderHandler);
            blue_slider.addEventListener(SliderEvent.THUMB_DRAG,sliderHandler);
            blue_slider.addEventListener(Event.CHANGE,sliderHandler);

            // Respond to textfield events
            hexc1_txt.addEventListener(Event.CHANGE,changeHexHandler);
            hexc2_txt.addEventListener(Event.CHANGE,changeHexHandler);
            hexc3_txt.addEventListener(Event.CHANGE,changeHexHandler);
            c1_txt.addEventListener(Event.CHANGE,changeRGBHandler);
            c2_txt.addEventListener(Event.CHANGE,changeRGBHandler);
            c3_txt.addEventListener(Event.CHANGE,changeRGBHandler);

            // Draw a frame later
            addEventListener(Event.ENTER_FRAME,draw);


        }





        //*************************
        // Initialization:

        protected function draw(event:Event):void
        {




            // Set color transformations
            swatch1_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,127,0,97);
            swatch2_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,130,0,0);
            swatch3_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,10,106,0);
            swatch4_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,0,51,51);
            swatch5_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,0,51,102)
            swatch6_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,127,127,127);
            previewBox_btn.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);

            // Handle listeners...
            removeEventListener(Event.ENTER_FRAME,draw);
        }

        //*************************
        // Event Handling:

        protected function dragPressHandler( event:MouseEvent ):void
        {
            activeSwatch = new Swatch();
            activeSwatch.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
            activeSwatch.addEventListener(MouseEvent.MOUSE_UP,dragReleaseHandler);          
            activeSwatch.startDrag(true);
            addChild(activeSwatch);
        }

        protected function dragReleaseHandler( event:MouseEvent ):void
        {
            if( activeSwatch.hitTestObject(car) ){
                car.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
            }
            activeSwatch.stopDrag();
            activeSwatch.removeEventListener(MouseEvent.MOUSE_UP,dragReleaseHandler);           
            removeChild(activeSwatch);
        }

        protected function clickHandler(event:MouseEvent):void
        {
            car.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
        }

        protected function changeRGBHandler(event:Event):void
        {
            c1 = Number(c1_txt.text);
            c2 = Number(c2_txt.text);
            c3 = Number(c3_txt.text);

            if(!(c1>=0)){
                c1 = 0;
            }
            if(!(c2>=0)){
                c2 = 0;
            }
            if(!(c3>=0)){
                c3 = 0;
            }
            hexc1_txt.text = c1.toString(16);
            hexc2_txt.text = c2.toString(16);
            hexc3_txt.text = c3.toString(16);

            updateSliders();
        }

        protected function changeHexHandler(event:Event):void
        {
            c1 = parseInt("0x"+hexc1_txt.text);
            c2 = parseInt("0x"+hexc2_txt.text);
            c3 = parseInt("0x"+hexc3_txt.text);

            if(!(c1>=0)){
                c1 = 0;
            }
            if(!(c2>=0)){
                c2 = 0;
            }
            if(!(c3>=0)){
                c3 = 0;
            }
            c1_txt.text = c1.toString();
            c2_txt.text = c2.toString();
            c3_txt.text = c3.toString();

            updateSliders();
        }

        protected function sliderHandler( event:SliderEvent ):void
        {
            switch( event.target )
            {
                case red_slider:

                    c1 = red_slider.value;
                    break;

                case green_slider:

                    c2 = green_slider.value;
                    break;

                case blue_slider:

                    c3 = blue_slider.value;
                    break;
            }
            updateFields();
        }

        protected function swatchHandler(event:MouseEvent):void
        {
            switch( event.target.parent )
            {
                case swatch1_btn:

                    c1 = 127;
                    c2 = 0;
                    c3 = 97;
                    break;

                case swatch2_btn:

                    c1 = 130;
                    c2 = 0;
                    c3 = 0;
                    break;

                case swatch3_btn:

                    c1 = 10;
                    c2 = 106;
                    c3 = 0;
                    break;

                case swatch4_btn:

                    c1 = 0;
                    c2 = 51;
                    c3 = 51;
                    break;

                case swatch5_btn:

                    c1 = 0;
                    c2 = 51;
                    c3 = 102;
                    break;

                case swatch6_btn:

                    c1 = 127;
                    c2 = 127;
                    c3 = 127;
                    break;
            }
            updateFields();
            updateSliders();
        }

        //*************************
        // Utils:

        protected function updateFields():void
        {
            hexc1_txt.text = c1.toString(16);
            hexc2_txt.text = c2.toString(16);
            hexc3_txt.text = c3.toString(16);

            c1_txt.text = c1.toString();
            c2_txt.text = c2.toString();
            c3_txt.text = c3.toString();

            previewBox_btn.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
        }

        protected function updateSliders():void
        {
            red_slider.value = c1;
            green_slider.value = c2;
            blue_slider.value = c3;

            previewBox_btn.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
        }


    }

}

2 个答案:

答案 0 :(得分:0)

问题是当你加载/调用这个类时它正在寻找舞台上的对象,如果它们不在那里就会抛出错误。所以你不应该调用它,直到它与它正在寻找的项目在同一帧上。因此,如果您要查找的所有项目都在第4帧,那么您应该在其中调用它。

所以转到第4帧并添加以下代码:

var myDrawings5 = new Drawing5();

另一个选择是将当前构造函数中的代码移动到名为init()的新公共函数中。然后你可以随时加载类,但是在项目在舞台上之前你不要调用init()。

public function Drawing5(){
  // now emtpy
}

public function init(){
  // move code from Drawing5() here
}

然后加载并在需要的地方调用

var myDrawings5 = new Drawing5();
myDrawings5.init();

答案 1 :(得分:0)

我认为您的问题是之前使用Drawing5作为文档类。现在你想在第4帧中单独运行它。为此,你需要为你的swfs阶段提供一个引用,以便它知道在哪里找到所有这些对象。

var d5:Drawing5 = new Drawing5(this);

然后向Drawing5类添加一个名为_container(或其他东西)的属性

private var _container:MovieClip;

然后在Drawing 5构造函数中,更改它以适应新的var并设置容器

public function Drawing5(container:MovieClip){
    _container = container;
}

然后在Drawing5中,更改引用像列表或按钮这样的对象的所有调用,将容器包含在路径中,如下所示:

// Respond to mouse events
_container.swatch1_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);

我希望我能正确理解你的问题,这会有所帮助。