Action Script ExternalInterface.call()问题

时间:2012-03-20 10:24:17

标签: flash actionscript

我使用以下AS代码调用javascript函数,该函数将返回所需的值。

package 
{
    import flash.display.Sprite;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.MouseEvent;
    import flash.net.FileReference;
    import flash.utils.ByteArray;
    import com.adobe.images.JPGEncoder;
    import flash.external.ExternalInterface;

    public class Main extends Sprite
    {
        private var camera:Camera = Camera.getCamera();
        private var video:Video = new Video();
        private var bmd:BitmapData = new BitmapData(320,240);
        private var bmp:Bitmap;
        private var fileReference:FileReference = new FileReference();
        private var byteArray:ByteArray;
        private var jpg:JPGEncoder = new JPGEncoder();
        var id:int;





        public function Main():void
        {

            saveButton.visible = false;
            discardButton.visible = false;

            saveButton.addEventListener(MouseEvent.MOUSE_UP, saveImage);
            discardButton.addEventListener(MouseEvent.MOUSE_UP, discard);
            capture.addEventListener(MouseEvent.MOUSE_UP, captureImage);

            if (camera != null)
            {
                video.smoothing = true;
                video.attachCamera(camera);
                video.x = 140;
                video.y = 40;
                addChild(video);
            }
            else
            {
                trace("No Camera Detected");
            }
        }

        private function captureImage(e:MouseEvent):void
        {
            bmd.draw(video);
            bmp = new Bitmap(bmd);
            bmp.x = 140;
            bmp.y = 40;
            addChild(bmp);

            capture.visible = false;
            saveButton.visible = true;
            discardButton.visible = true;
        }

        private function saveImage(e:MouseEvent):void
        {
            byteArray = jpg.encode(bmd);
            id=ExternalInterface.call("getID()");

            fileReference.save(byteArray, id+".jpg");

            removeChild(bmp);
            saveButton.visible = false;
            discardButton.visible = false;
            capture.visible = true;
        }

        private function discard(e:MouseEvent):void
        {
            removeChild(bmp);
            saveButton.visible = false;
            discardButton.visible = false;
            capture.visible = true;
        }
    }
}

然而,当调用该函数时,它似乎正在给我一个0总是,,,,,试图很难理解这个问题,但我想我无法弄明白。我试图提醒返回的值通过javascript函数,它是正确的,

function getID() {

    var idno = $('#ctl00_ContentPlaceHolder1_memberidcam').val();
    alert(idno);
    return idno;

}

请有人帮助我。 感谢

2 个答案:

答案 0 :(得分:2)

从嵌入代码中检查您的AllowScriptAccess参数。它会影响是否允许swf通过ExternalInterface与JS交互:

http://helpx.adobe.com/flash/kb/control-access-scripts-host-web.html

此外,最佳做法是检查ExternalInterface是否可用:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#available

答案 1 :(得分:2)

这可能是您从JavaScript返回的类型的问题。 val()方法可能只是返回一个字符串。

因此,请尝试更改JavaScript以将值转换为整数:

function getID() {
    var idno = $('#ctl00_ContentPlaceHolder1_memberidcam').val();
    return parseInt(idno);
}