将我的动作脚本转换为类?

时间:2010-01-25 14:01:54

标签: actionscript-3

我如何将我的动作脚本3(在时间轴上)转换为课程?因为我在多个FLA文件中使用相同的功能。

// IMPORTS

import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;


// VARIABLES

mcToolTip.toolTip.blendMode = BlendMode.LAYER;
mcToolTip.toolTip.text = "";
var toolio = false;
var settext;

var urlButton1:String = 'URL STRING 1';
var urlButton2:String = 'URL STRING 1';
var urlButton3:String = 'URL STRING 1';
var urlButton4:String = 'URL STRING 1';
var urlButton5:String = 'URL STRING 1';

// MISC

mcButton1.stop();
mcButton2.stop();
mcButton3.stop();
mcButton4.stop();
mcButton5.stop();

mcButton1.buttonMode = true;
mcButton2.buttonMode = true;
mcButton3.buttonMode = true;
mcButton4.buttonMode = true;
mcButton5.buttonMode = true;

// EVENT LISTENERS

//button1
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton1.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton1.addEventListener(MouseEvent.MOUSE_OVER,button1Text);
mcButton1.addEventListener(MouseEvent.MOUSE_DOWN,callButton1);
mcButton1.addEventListener(MouseEvent.CLICK,mclick);

//button2
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton2.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton2.addEventListener(MouseEvent.MOUSE_OVER,button2Text);
mcButton2.addEventListener(MouseEvent.MOUSE_DOWN,callButton2);
mcButton2.addEventListener(MouseEvent.CLICK,mclick);

//button3
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton3.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton3.addEventListener(MouseEvent.MOUSE_OVER,button3Text);
mcButton3.addEventListener(MouseEvent.MOUSE_DOWN,callButton3);
mcButton3.addEventListener(MouseEvent.CLICK,mclick);

//button4
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton4.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton4.addEventListener(MouseEvent.MOUSE_OVER,button4Text);
mcButton4.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
mcButton4.addEventListener(MouseEvent.CLICK,mclick);

//button5
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcButton5.addEventListener(MouseEvent.MOUSE_OUT,mout);
mcButton5.addEventListener(MouseEvent.MOUSE_OVER,button5Text);
mcButton5.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
mcButton5.addEventListener(MouseEvent.CLICK,mclick);

// FUNCTIONS

function mclick(e:MouseEvent):void {
    toolio = true;
    e.currentTarget.gotoAndStop(5);
    e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT,mout);
    e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER,mover);
    settext = mcToolTip.toolTip.text;

    if (e.currentTarget !== mcButton2) {
        mcButton2.addEventListener(Event.ENTER_FRAME, playReverse);
        mcButton2.addEventListener(MouseEvent.MOUSE_OUT, mout);
        mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
    }

    if (e.currentTarget !== mcButton3) {
        mcButton3.addEventListener(Event.ENTER_FRAME, playReverse);
        mcButton3.addEventListener(MouseEvent.MOUSE_OUT, mout);
        mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
    }

    if (e.currentTarget !== mcButton4) {
        mcButton4.addEventListener(Event.ENTER_FRAME, playReverse);
        mcButton4.addEventListener(MouseEvent.MOUSE_OUT, mout);
        mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
    }

    if (e.currentTarget !== mcButton1) {
        mcButton1.addEventListener(Event.ENTER_FRAME, playReverse);
        mcButton1.addEventListener(MouseEvent.MOUSE_OUT, mout);
        mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
    }

    if (e.currentTarget !== mcButton5) {
        mcButton5.addEventListener(Event.ENTER_FRAME, playReverse);
        mcButton5.addEventListener(MouseEvent.MOUSE_OUT, mout);
        mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
    }
}

function mover(e:MouseEvent):void {
    stopPlayReverse(e.currentTarget as MovieClip);
    e.currentTarget.play();
    var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
}

function mout(e:MouseEvent):void {
    var mc:MovieClip = e.currentTarget as MovieClip; 
    if (mc !== null) {
        mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
    }
    if ( toolio == false ) {
        var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
    }
    if (settext != undefined) {
        mcToolTip.toolTip.text = settext;
    }
}

function playReverse(e:Event):void {
    var mc:MovieClip = e.currentTarget as MovieClip; 

    if (mc.currentFrame == 1) {
        stopPlayReverse(mc);
    } else {
        mc.prevFrame();
    }
}

function stopPlayReverse(mc:MovieClip):void {
  if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
    mc.removeEventListener(Event.ENTER_FRAME, playReverse);
  }
}

function button1Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 1"; }
function button2Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 2"; }
function button3Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 3"; }
function button4Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 4"; }
function button5Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 5"; }

function callButton1(evt:MouseEvent):void { ExternalInterface.call("button1", urlButton1);}
function callButton2(evt:MouseEvent):void { ExternalInterface.call("button2", urlButton2);}
function callButton3(evt:MouseEvent):void { ExternalInterface.call("button3", urlButton3); }
function callButton4(evt:MouseEvent):void { ExternalInterface.call("button4", urlButton4);}
function callButton5(evt:MouseEvent):void { ExternalInterface.call("button5", urlButton5);}

2 个答案:

答案 0 :(得分:1)

首先,您的设置中似乎已选中“自动声明舞台实例”。我像瘟疫一样避免这种情况。按照here的说明:在“禁用舞台实例自动声明”下。取消选中该框将强制您声明其他舞台实例,如mcToolTip和mcButtons。但它将有助于未来的发展努力(相信我。)

无论如何,我认为这将是你的文档类,因为你说它是按照swf使用的。我把它命名为MyFirstClass,你可能想要一个更好的名字。您需要偶然“BOTH”该名称的实例...类签名中的一个“公共类MyFirstClass扩展MovieClip”,构造函数中的第二个(与类同名的函数)一旦运行就会运行class是“instantiated

将此文件放在与.fla相同的目录中。添加“MyFirstClass”作为文档类。

阅读有关如何编写自己的课程的更多信息:here

package {
    import fl.transitions.*;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import flash.external.ExternalInterface;
    impprt flash.display.MovieClip;

    public class MyFirstClass extends MovieClip {
        public var toolio = false;
        public var settext;

        public var urlButton1:String = 'URL STRING 1';
        public var urlButton2:String = 'URL STRING 1';
        public var urlButton3:String = 'URL STRING 1';
        public var urlButton4:String = 'URL STRING 1';
        public var urlButton5:String = 'URL STRING 1';

        public function MyFirstClass():void {

            mcToolTip.toolTip.blendMode = BlendMode.LAYER;
            mcToolTip.toolTip.text = "";

            mcButton1.stop();
            mcButton2.stop();
            mcButton3.stop();
            mcButton4.stop();
            mcButton5.stop();

            mcButton1.buttonMode = true;
            mcButton2.buttonMode = true;
            mcButton3.buttonMode = true;
            mcButton4.buttonMode = true;
            mcButton5.buttonMode = true;

            // EVENT LISTENERS

            //button1
            mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
            mcButton1.addEventListener(MouseEvent.MOUSE_OUT,mout);
            mcButton1.addEventListener(MouseEvent.MOUSE_OVER,button1Text);
            mcButton1.addEventListener(MouseEvent.MOUSE_DOWN,callButton1);
            mcButton1.addEventListener(MouseEvent.CLICK,mclick);

            //button2
            mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
            mcButton2.addEventListener(MouseEvent.MOUSE_OUT,mout);
            mcButton2.addEventListener(MouseEvent.MOUSE_OVER,button2Text);
            mcButton2.addEventListener(MouseEvent.MOUSE_DOWN,callButton2);
            mcButton2.addEventListener(MouseEvent.CLICK,mclick);

            //button3
            mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
            mcButton3.addEventListener(MouseEvent.MOUSE_OUT,mout);
            mcButton3.addEventListener(MouseEvent.MOUSE_OVER,button3Text);
            mcButton3.addEventListener(MouseEvent.MOUSE_DOWN,callButton3);
            mcButton3.addEventListener(MouseEvent.CLICK,mclick);

            //button4
            mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
            mcButton4.addEventListener(MouseEvent.MOUSE_OUT,mout);
            mcButton4.addEventListener(MouseEvent.MOUSE_OVER,button4Text);
            mcButton4.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
            mcButton4.addEventListener(MouseEvent.CLICK,mclick);

            //button5
            mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
            mcButton5.addEventListener(MouseEvent.MOUSE_OUT,mout);
            mcButton5.addEventListener(MouseEvent.MOUSE_OVER,button5Text);
            mcButton5.addEventListener(MouseEvent.MOUSE_DOWN,callButton5);
            mcButton5.addEventListener(MouseEvent.CLICK,mclick);
        }

        // FUNCTIONS

        public function mclick(e:MouseEvent):void {
            toolio = true;
            e.currentTarget.gotoAndStop(5);
            e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT,mout);
            e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER,mover);
            settext = mcToolTip.toolTip.text;

            if (e.currentTarget !== mcButton2) {
                mcButton2.addEventListener(Event.ENTER_FRAME, playReverse);
                mcButton2.addEventListener(MouseEvent.MOUSE_OUT, mout);
                mcButton2.addEventListener(MouseEvent.MOUSE_OVER,mover);
            }

            if (e.currentTarget !== mcButton3) {
                mcButton3.addEventListener(Event.ENTER_FRAME, playReverse);
                mcButton3.addEventListener(MouseEvent.MOUSE_OUT, mout);
                mcButton3.addEventListener(MouseEvent.MOUSE_OVER,mover);
            }

            if (e.currentTarget !== mcButton4) {
                mcButton4.addEventListener(Event.ENTER_FRAME, playReverse);
                mcButton4.addEventListener(MouseEvent.MOUSE_OUT, mout);
                mcButton4.addEventListener(MouseEvent.MOUSE_OVER,mover);
            }

            if (e.currentTarget !== mcButton1) {
                mcButton1.addEventListener(Event.ENTER_FRAME, playReverse);
                mcButton1.addEventListener(MouseEvent.MOUSE_OUT, mout);
                mcButton1.addEventListener(MouseEvent.MOUSE_OVER,mover);
            }

            if (e.currentTarget !== mcButton5) {
                mcButton5.addEventListener(Event.ENTER_FRAME, playReverse);
                mcButton5.addEventListener(MouseEvent.MOUSE_OUT, mout);
                mcButton5.addEventListener(MouseEvent.MOUSE_OVER,mover);
            }
        }

        public function mover(e:MouseEvent):void {
            stopPlayReverse(e.currentTarget as MovieClip);
            e.currentTarget.play();
            var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
        }

        public function mout(e:MouseEvent):void {
            var mc:MovieClip = e.currentTarget as MovieClip; 
            if (mc !== null) {
                mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
            }
            if ( toolio == false ) {
                var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
            }
            if (settext != undefined) {
                mcToolTip.toolTip.text = settext;
            }
        }

        public function playReverse(e:Event):void {
            var mc:MovieClip = e.currentTarget as MovieClip; 

            if (mc.currentFrame == 1) {
                stopPlayReverse(mc);
            } else {
                mc.prevFrame();
            }
        }

        public function stopPlayReverse(mc:MovieClip):void {
          if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
            mc.removeEventListener(Event.ENTER_FRAME, playReverse);
          }
        }

        public function button1Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 1"; }
        public function button2Text(e:MouseEvent):void { mcToolTip.toolTip.text = "Menu 2"; }
        public function button3Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 3"; }
        public function button4Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 4"; }
        public function button5Text(e:MouseEvent):void {   mcToolTip.toolTip.text = "Menu 5"; }

        public function callButton1(evt:MouseEvent):void { ExternalInterface.call("button1", urlButton1);}
        public function callButton2(evt:MouseEvent):void { ExternalInterface.call("button2", urlButton2);}
        public function callButton3(evt:MouseEvent):void { ExternalInterface.call("button3", urlButton3); }
        public function callButton4(evt:MouseEvent):void { ExternalInterface.call("button4", urlButton4);}
        public function callButton5(evt:MouseEvent):void { ExternalInterface.call("button5", urlButton5);}      
    }
}

答案 1 :(得分:0)

我想尽可能地概括。你通过调用带有数字的按钮和动画片段来做一件好事,但是你错过了这样做的一个优点,那就是你现在可以概括事件处理程序。

这是我通过编辑sberry2A发布的代码来提出的:

package {
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;
import flash.display.MovieClip;

public class MyFirstClass extends MovieClip {
    public var toolio = false;
    public var settext;

    public var buttonSettings :Object;

    public function MyFirstClass():void {
        buttonSettings = {
            1: {
                url:     "http://example.org",
                tooltip: "Some text here"
            },
            2: {
                url:     "http://example.org",
                tooltip: "Some text here"
            },
            3: {
                url:     "http://example.org",
                tooltip: "Some text here"
            },
            4: {
                url:     "http://example.org",
                tooltip: "Some text here"
            },
            5: {
                url:     "http://example.org",
                tooltip: "Some text here"
            }
        };

        mcToolTip.toolTip.blendMode = BlendMode.LAYER;
        mcToolTip.toolTip.text = "";

        initializeButtons();
        addEventListeners();
    }
    // FUNCTIONS
    public function initializeButtons():void {
        for(var i:String in buttonSettings) {
            this["mcButton"+i].stop();

            this["mcButton"+i].mouseChildren = false;
            this["mcButton"+i].buttonMode    = true;
            this["mcButton"+i].useHandCursor = true;
        }
    }
    public function addEventListeners():void {
        for(var i:String in buttonSettings) {
            this["mcButton"+i].addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
            this["mcButton"+i].addEventListener(MouseEvent.MOUSE_OUT,handleMouseOut);
            this["mcButton"+i].addEventListener(MouseEvent.CLICK,handleMouseClick);
        }
    }
    public function addPlayReverseListenerButSkip(skip:String = null):void {
        for(var i:String in buttonSettings) {
            if(skip && skip == i) { continue; }
            this["mcButton"+i].addEventListener(Event.ENTER_FRAME, playReverse);
        }
    }
    public function handleMouseClick(e:MouseEvent):void {
        toolio = true;
        e.target.gotoAndStop(5);
        e.target.removeEventListener(MouseEvent.MOUSE_OUT,mout);
        e.target.removeEventListener(MouseEvent.MOUSE_OVER,mover);
        settext = mcToolTip.toolTip.text;

        var targetNumber :String = e.target.name.split("mcButton").join("");
        addPlayReverseListenerButSkip(targetNumber);

        if(ExternalInterface.available) {
            ExternalInterface.call("button" + targetNumber, buttonSettings[targetNumber].url);
        }
    }
    public function handleMouseOver(e:MouseEvent):void {
        var mc :MovieClip = e.target as MovieClip;
        stopPlayReverse(mc);
        mc.play();

        var targetNumber = e.target.name.split("mcButton").join("");
        setTooltipTextTo(targetNumber);

        var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
    }
    public function handleMouseOut(e:MouseEvent):void {
        var mc:MovieClip = e.currentTarget as MovieClip; 
        if (mc !== null) {
            mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
        }
        if ( toolio == false ) {
            var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
        }
        if (settext != undefined) {
            mcToolTip.toolTip.text = settext;
        }
    }
    public function stopPlayReverse(mc:MovieClip) {
        if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
            mc.removeEventListener(Event.ENTER_FRAME, playReverse);
        }
    }
    public function playReverse(e:Event):void {
        var mc :MovieClip = e.target as MovieClip; 

        if (mc.currentFrame == 1) {
            stopPlayReverse(mc);
        } 
        else {
            mc.prevFrame();
        }
    }
    public function setTooltipTextTo(targetNumber:String):void {
        mcToolTip.toolTip.text = buttonSettings[targetNumber].tooltip;
    }
}

}

此代码未经过测试,但通过一些调试,您应该能够像上面的代码一样工作。