【问题标题】:ActionScript + JavaScript动作脚本 + JavaScript
【发布时间】:2020-09-29 05:52:28
【问题描述】:

我想从嵌入的 .swf 文件调用 JavaScript 函数。具体来说,我想在我的一个外部链接的 JavaScript 文件中调用一个函数:

function loadTrack(){

    //Radio Mode feature by nosferathoo, more info in: https://sourceforge.net/tracker/index.php?func=detail&aid=1341940&group_id=128363&atid=711474

    if (radio_mode && track_index == playlist_size - 1) {
        playlist_url=playlist_array[track_index].location;
        for (i=0;i<playlist_mc.track_count;++i) {
            removeMovieClip(playlist_mc.tracks_mc["track_"+i+"_mc"]);
        }
        playlist_mc.track_count=0;
        playlist_size=0;
        track_index=0;
        autoload=true;
        autoplay=true;
        loadPlaylist();
        return(0);
    }
    start_btn_mc.start_btn._visible = false;
    track_display_mc.display_txt.text = playlist_array[track_index].label;
    
    if (track_display_mc.display_txt._width > track_display_mc.mask_mc._width) {
        track_display_mc.onEnterFrame = scrollTitle;
    }else{
        track_display_mc.onEnterFrame = null;
        track_display_mc.display_txt._x = 0;
    }
    mysound.loadSound(playlist_array[track_index].location,true);
    play_mc.gotoAndStop(2)

    //info button
    if(playlist_array[track_index].info!=undefined){
        info_mc._visible = true;
        info_mc.info_btn.onPress = function(){
            getURL(playlist_array[track_index].info,"_blank")
        }
        info_mc.info_btn.onRollOver = function(){
            track_display_mc.display_txt.text = info_button_text;
        }
        info_mc.info_btn.onRollOut = function(){
            track_display_mc.display_txt.text = playlist_array[track_index].label;
        }
    }else{
        info_mc._visible = false;
    }
    resizeUI();
    _root.onEnterFrame=function(){
        //HACK doesnt need to set the volume at every enterframe
        mysound.setVolume(this.volume_level)
        var load_percent = (mysound.getBytesLoaded()/mysound.getBytesTotal())*100
        track_display_mc.loader_mc.load_bar_mc._xscale = load_percent;
        if(mysound.getBytesLoaded()==mysound.getBytesTotal()){
            //_root.onEnterFrame = null;
        }
    }
}

它在一个 .as 文件中,我假设它以某种方式变成了 swf 文件。我将如何处理这个和re-compile .as 文件?

【问题讨论】:

    标签: javascript actionscript flash


    【解决方案1】:

    让我们使用 JS 注入和 ExternalInterface 为 AS2 和 AS3 编译这些答案(两种方式都适用于两种语言)

    AS2:

    
    // to use javascript injection in a url request
    getURL("javascript:displayPost(" + postId + "," + feedId +");", "_self");
    
    // to use the external interface
    import flash.external.ExternalInterface;
    ExternalInterface.call("displayPost",postId,feedId);
    

    AS3:

    
    // to use javascript injection in a url request
    navigateToURL(new URLRequest("javascript:displayPost(" + postId + "," + feedId +");"), "_self");
    
    // to use the external interface
    import flash.external.ExternalInterface;
    ExternalInterface.call("displayPost",postId,feedId);
    

    请注意,在 AS2 和 AS3 中,ExternalInterface 方法完全相同(在 Flash 8 中为 AS2 引入了 ExternalInterface)。在 AS2 和 AS3 中,javascript 注入方法是相同的,只是它是 navigateToURL 而不是 getURL,并且 url 字符串被包裹在 new URLRequest() 中,因为它需要一个 URLRequest 对象。此外,在使用 javascript 注入时,最好将目标窗口设置为“_self”以避免打开新的选项卡或窗口。

    【讨论】:

      【解决方案2】:

      如果将来有人在看这个问题,那么 Actionscript 3 版本的 altCognito 的答案是这样的:

      ExternalInterface.call("displayPost",postId,feedId);
      

      【讨论】:

        【解决方案3】:
          getURL("javascript:displayPost(" + postId + "," + feedId +")");
        

        发件人:

        您还可以查看以下内容:

        http://osflash.org/projects/flashjs/tutorials/jstoas

        【讨论】:

        • 哇!超快速响应!我现在无法测试它,但看起来它应该可以工作。谢谢!
        • 我假设 postId 和 feedId 是 displayPost 的参数?
        猜你喜欢
        • 2016-02-28
        • 2019-05-14
        • 2010-12-09
        • 2010-12-20
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        • 2016-01-23
        • 1970-01-01
        相关资源
        最近更新 更多