使用ExternalInterface访问Flash中未定义的属性错误

问题描述

| 我是Flash新手,因此解决此问题的方法可能很简单。不幸的是,我一直无法找到有效的解决方案,因此希望你们能提供帮助。我正在使用第三方闪光灯-Coverflow开发一个相册的网站。它看起来很棒,您可以在这里下载它:http://www.weberdesignlabs.com/blog/2009/12/flash-10-coverflow/。我面临的挑战是,我想通过HTML中的Javascript调用来处理专辑。在我的搜索中,我发现了ExternalInterface,但无法使其正常工作。当我尝试导出电影时,出现“访问未定义属性gotoCoverFlowItem \”错误。 下面是我尝试修改的Coverflow.ai文件中包含的代码,并对其进行了修改。完整的代码可以在上面的链接中找到。我正在使用Flash 10版本。
////////////////////////////////////////////
// Project: Flash 10 Coverflow
// Date: 10/3/09
// Author: Stephen Weber
////////////////////////////////////////////
package {

    ////////////////////////////////////////////
    // IMPORTS
    ////////////////////////////////////////////

    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.MovieClip;
    import flash.display.BlendMode;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.events.IOErrorEvent;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.geom.ColorTransform;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.net.navigatetoURL;
    import flash.display.Stage;
    import flash.utils.setTimeout;
    import flash.external.ExternalInterface;

    //TweenLite - Tweening Engine - SOURCE: http://blog.greensock.com/tweenliteas3/
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.greensock.plugins.*;

    public class Coverflow extends Sprite {

        ////////////////////////////////////////////
        // VARIABLES
        ////////////////////////////////////////////

        // size of the stage
        private var sw:Number;
        private var sh:Number;

        private var background:Background;

        // padding between each cover,can be customed via xml
        private var coverflowSpacing:Number=30;

        // transition time for movement
        private var transitionTime:Number=0.75;

        // the center of the stage
        private var centerX:Number;
        private var centerY:Number;

        // store each image cover\'s instance
        private var coverArray:Array=new Array();

        // title of each image
        private var coverLabel:CoverflowTitle = new CoverflowTitle();

        // the slider under the image cover
        private var coverSlider:Scrollbar;

        // how many image covers
        private var coverflowItemsTotal:Number;

        // how to open the link
        private var _target:String;

        // size of the image cover
        private var coverflowImageWidth:Number;

        private var coverflowImageHeight:Number;

        //Holds the objects in the data array
        private var _data:Array = new Array();

        // the y position of the item\'s title
        private var coverLabelPositionY:Number;

        //Z Position of Current CoverflowItem
        private var centerCoverflowZPosition:Number=-125;

        // display the middle of the cover or not
        private var startIndexInCenter:Boolean=true;

        // which cover to display in the beginning
        private var startIndex:Number=0;

        // the slide\'s Y position
        private var coverSlidePositionY:Number;

        //Holder for current CoverflowItem
        private var _currentCover:Number;

        //CoverflowItem Container
        private var coverflowItemContainer:Sprite = new Sprite();

        //XML Loading
        private var coverflowXMLLoader:URLLoader;

        //XML
        private var coverflowXML:XML;

        // the image cover\'s white border padding
        private var padding:Number=4;

        // stage reference
        private var _stage:Stage;

        //reflection
        private var reflection:Reflect;

        //Reflection Properties
        private var reflectionAlpha:Number;

        private var reflectionRatio:Number;

        private var reflectiondistance:Number;

        private var reflectionUpdateTime:Number;

        private var reflectionDropoff:Number;

        ////////////////////////////////////////////
        // CONSTRUCTOR - INITIAL ACTIONS
        ////////////////////////////////////////////
        public function Coverflow(_width:Number,_height:Number,__stage:Stage = null):void {
            _stage=__stage;
            sw=_width;
            sh=_height;
            centerX=_width>>1;
            centerY=(_height>>1) - 20;
            loadXML();

            //Grabs Background color passed in through FlashVars
            var backgColor:String = _stage.loaderInfo.parameters[\"backgroundColor\"];

            if(backgColor == null) {
                //Black
                backgColor = \"0x000000\";

                //White
                //backgColor = \"0xFFFFFF\";
            }

            //Creates Background MovieClip
            background = new Background();

            //Set Background To Provided Width/Height
            background.width = _width;
            background.height = _height;

            //Adds background MovieClip to displayList
            addChild(background);

            //Tints Background MovieClip with provided tint
            TweenPlugin.activate([TintPlugin]);
            TweenLite.to(background,{tint:backgColor});

            //Grabs Background color passed in through FlashVars
            var labelColor:String = _stage.loaderInfo.parameters[\"labelColor\"];

            //Check for value and then default
            if(labelColor == null) {
                //Black
                //labelColor = \"0x000000\";

                //White
                labelColor = \"0xFFFFFF\";
            }

            //Tint Coverflow label to color provided
            TweenLite.to(coverLabel,{tint:labelColor});

            if (_stage) {
                _stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
            }
        }
        ////////////////////////////////////////////
        // FUNCTIONS
        ////////////////////////////////////////////

        private function keyDownHandler(e:KeyboardEvent):void {
            if (e.keyCode==37||e.keyCode==74) {
                clickPre();
            }
            if (e.keyCode==39||e.keyCode==75) {
                clickNext();
            }
            // 72 stand for \"H\" key,191 stand for \"?\" key
            if (e.keyCode==72||e.keyCode==191) {

            }
        }

        // display the prevIoUs image
        private function clickPre(e:Event=null):void {
            _currentCover--;
            if (_currentCover<0) {
                _currentCover=coverflowItemsTotal-1;
            }
            coverSlider.value=_currentCover;
            gotoCoverflowItem(_currentCover);
        }

        // display the next image

        private function clickNext(e:Event=null):void {
            _currentCover++;
            if (_currentCover>coverflowItemsTotal-1) {
                _currentCover=0;
            }
            coverSlider.value=_currentCover;
            gotoCoverflowItem(_currentCover);
        }

        // loading the XML
        private function loadXML():void {

            //Loads XML passed through FlashVars
            var xml_source:String = _stage.loaderInfo.parameters[\"xmlPath\"];

            //If XML not found through FlashVars then defaults to xml path below
            if(xml_source == null) {
                xml_source = \'xml/data.xml\';
            }

            // loading the cover xml here
            coverflowXMLLoader = new URLLoader();
            coverflowXMLLoader.load(new URLRequest(\"xml/data.xml\"));
            coverflowXMLLoader.addEventListener(Event.COMPLETE,coverflowXMLLoader_Complete);
            coverflowXMLLoader.addEventListener(IOErrorEvent.IO_ERROR,coverflowXMLLoader_IOError);

        }

        // parse the XML
        private function coverflowXMLLoader_Complete(e:Event):void {
            coverflowXML=new XML(e.target.data);
            coverflowItemsTotal=coverflowXML.cover.length();
            coverflowSpacing=Number(coverflowXML.@coverflowSpacing);
            coverflowImageWidth=Number(coverflowXML.@imageWidth);
            coverflowImageHeight=Number(coverflowXML.@imageHeight);
            coverLabelPositionY=Number(coverflowXML.@coverLabelPositionY);
            coverSlidePositionY=Number(coverflowXML.@coverSlidePositionY);
            transitionTime=Number(coverflowXML.@transitionTime);
            centerCoverflowZPosition=Number(coverflowXML.@centerCoverflowZPosition);

            //Image Border
            padding = Number(coverflowXML.@imagePadding)

            //Reflection Attributes
            reflectionAlpha=Number(coverflowXML.@reflectionAlpha);
            reflectionRatio=Number(coverflowXML.@reflectionRatio);
            reflectiondistance=Number(coverflowXML.@reflectiondistance);
            reflectionUpdateTime=Number(coverflowXML.@reflectionUpdateTime);
            reflectionDropoff=Number(coverflowXML.@reflectionDropoff);

            startIndex=Number(coverflowXML.@startIndex);
            startIndexInCenter = ([email protected]().toString()==\"yes\");
            [email protected]();

            for (var i=0; i<coverflowItemsTotal; i++) {

                //Make An Object To Hold Values
                var _obj:Object = new Object();

                //Set Values To Object from XML for each CoverflowItem
                _obj.image = (coverflowXML.cover[i][email protected]());
                _obj.title = (coverflowXML.cover[i][email protected]());
                _obj.link = (coverflowXML.cover[i][email protected]());
                _data[i] = _obj;

            }
            loadCover();
        }

        private function coverflowXMLLoader_IOError(event:IOErrorEvent):void {
            trace(\"Coverflow XML Load Error: \"+ event);
        }

        // load the image cover when xml is loaded
        private function loadCover():void {

            for (var i:int = 0; i < coverflowItemsTotal; i++) {
                var cover:Sprite=createCover(i,_data[i].image);
                coverArray[i]=cover;
                cover.y=centerY;
                cover.z=0;
                coverflowItemContainer.addChild(cover);
            }

            if (startIndexInCenter) {
                startIndex=coverArray.length>>1;
                gotoCoverflowItem(startIndex);

            } else {

                gotoCoverflowItem(startIndex);

            }
            _currentCover=startIndex;
            coverSlider=new Scrollbar(coverflowItemsTotal,_stage);
            coverSlider.value=startIndex;
            coverSlider.x = (_stage.stageWidth/2) - (coverSlider.width/2);
            coverSlider.y=_stage.stageHeight-40;
            coverSlider.addEventListener(\"UPDATE\",coverSlider_Update);
            coverSlider.addEventListener(\"PREVIoUS\",coverSlider_PrevIoUs);
            coverSlider.addEventListener(\"NEXT\",coverSlider_Next);
            addChild(coverSlider);

            //coverLabel.x = (sw - coverLabel.width)>>1;
            coverLabel.x = (_stage.stageWidth/2) - (coverLabel.width/2);
            coverLabel.y=coverLabelPositionY;
            addChild(coverLabel);

            addChild(coverSlider);
            addChild(coverLabel);

        }

        private function coverSlider_Update(e:Event):void {
            var value:Number=(coverSlider.value);
            gotoCoverflowItem(value);
            e.stopPropagation();
        }

        private function coverSlider_PrevIoUs(e:Event):void {
            clickPre();
        }

        private function coverSlider_Next(e:Event):void {
            clickNext();
        }

        // move to a certain cover via number
        private function gotoCoverflowItem(n:int):void {
            _currentCover=n;
            reOrderCover(n);
            if (coverSlider) {
                coverSlider.value=n;
            }
        }

        ExternalInterface.addCallback(\"gotoCover\",gotoCoverflowItem);

        private function cover_Selected(event:CoverflowItemEvent):void {

            var currentCover:uint=event.data.id;

            if (coverArray[currentCover].rotationY==0) {
                try {
                    // open the link if user click the cover in the middle again
                    if (_data[currentCover].link!=\"\") {
                        navigatetoURL(new URLRequest(_data[currentCover].link),_target);
                    }

                } catch (e:Error) {
                    //
                }

            } else {
                gotoCoverflowItem(currentCover);

            }

        }

        // change each cover\'s position and rotation
        private function reOrderCover(currentCover:uint):void {
            for (var i:uint = 0,len:uint = coverArray.length; i < len; i++) {
                var cover:Sprite=coverArray[i];

                if (i<currentCover) {
                    //Left Side
                    TweenLite.to(cover,transitionTime,{x:(centerX - (currentCover - i) * coverflowSpacing - coverflowImageWidth/2),z:(coverflowImageWidth/2),rotationY:-65});
                } else if (i > currentCover) {
                    //Right Side
                    TweenLite.to(cover,{x:(centerX + (i - currentCover) * coverflowSpacing + coverflowImageWidth/2),rotationY:65});
                } else {
                    //Center Coverflow
                    TweenLite.to(cover,{x:centerX,z:centerCoverflowZPosition,rotationY:0});

                    //Label Handling
                    coverLabel._text.text=_data[i].title;
                    coverLabel.alpha=0;
                    TweenLite.to(coverLabel,0.75,{alpha:1,delay:0.2});

                }
            }
            for (i = 0; i < currentCover; i++) {
                addChild(coverArray[i]);
            }
            for (i = coverArray.length - 1; i > currentCover; i--) {
                addChild(coverArray[i]);
            }

            addChild(coverArray[currentCover]);
            if (coverSlider) {
                addChild(coverSlider);
                addChild(coverLabel);
            }
        }

        //Create CoverflowItem and Set Data To It
        private function createCover(num:uint,url:String):Sprite {

            //Setup Data
            var _data:Object = new Object();
            _data.id=num;

            //Create CoverflowItem
            var cover:CoverflowItem=new CoverflowItem(_data);

            //Listen for Click
            cover.addEventListener(CoverflowItemEvent.COVERFLOWITEM_SELECTED,cover_Selected);

            //Set Some Values
            cover.name=num.toString();
            cover.image=url;
            cover.padding=padding;
            cover.imageWidth=coverflowImageWidth;
            cover.imageHeight=coverflowImageHeight;
            cover.setReflection(reflectionAlpha,reflectionRatio,reflectiondistance,reflectionUpdateTime,reflectionDropoff);

            //Put CoverflowItem in Sprite Container
            var coverItem:Sprite = new Sprite();
            cover.x=- coverflowImageWidth/2-padding;
            cover.y=- coverflowImageHeight/2-padding;
            coverItem.addChild(cover);
            coverItem.name=num.toString();

            return coverItem;
        }

    }
}
添加代码是:
ExternalInterface.addCallback(\"gotoCover\",gotoCoverflowItem);
请帮忙!     

解决方法

        将ExternalInterface调用放在Coverflow构造函数的末尾应可以正常工作。您当前所在的行不在函数内。 我不知道您的JavaScript是什么样,但是此代码需要gotoCoverflowItem的参数,因此我假设您正在传递适当的数据。 我没有遍历所有代码,但是请记住,当前代码可能具有某些功能,如果仅将其作为下一个封面的索引供稿,则可能无法完成。我确实在gotoColorflowItem函数中看到了代码,该代码根据选择内容更新了滑块和当前封面的变量,因此可能还不错。 另外,如果出于任何原因尝试在浏览器外部进行测试,则很可能会遇到运行时错误,因此您可能需要针对这种情况进行try / catch。 作者在页面上包括了他的智慧: \“注意:我不建议您尝试使用此文件学习Flash。很多人问如何更改文本颜色,如果您有这些要求,我建议您阅读一些Lynda.com Flash教程以使您熟悉使用Flash或雇用Flash开发人员来帮助您。\“     ,        好吧,您不能只是将ExternalInterface调用转储到您想要的任何位置。它必须位于诸如构造函数之类的函数内部。