开发者

Converting my actionscript into a class?

How would I go about converting my actionscript 3 (on the timeline) into a class? As I am using the same functionality across multiple FLA files.

// 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);
    }
   开发者_如何学Go 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);}


First thing, it appears you have "Automatically declare stage instances" checked in your settings. I avoid this like the plague. Follow the directions here: under "disabling stage instance auto-declaration." Unchecking that box will force you to declare your other stage instances, like mcToolTip and the mcButtons. But it will help in future development efforts (trust me.)

Anyway, I figured this would be your document class since you said it was used per swf. I named it MyFirstClass, you probably want a better name. You need to chance "BOTH" instances of that name... one in the class signature "public class MyFirstClass extends MovieClip" and the second in the constructor (the function with the same name as the class) which gets run as soon as the class is "instantiated"

Put this file in the same directory as your .fla. Add "MyFirstClass" as the document class.

Read up on more info about how to write your own class: 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);}      
    }
}


I like to generalize as much as possible. You did a good thing by calling the buttons and movieclips with numbers after them, but you're missing out on one of the advantages to doing that which is that you can now generalize event handlers.

This is what I came up with by editing the code sberry2A posted:

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;
    }
}

}

This code isn't tested, but with a little debugging, you should be able to get this working like the code above.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜