没有SWFObject的FlashVars不适用于纯AS3项目

时间:2011-09-02 09:58:32

标签: actionscript-3

这让我疯了。我过去曾与SWFObject合作过,这很棒。但是我要求不使用JavaScript。因此,当我尝试在网上做flashvars示例时,它们似乎对我不起作用。

重复步骤:

1)使用Flex或Flash Builder创建纯AS3项目

2)在index.html中,只要有.swf,添加名称值对后缀。 test.swf?富=酒吧

3)在主类Sprite的构造函数中,trace(root.loaderInfo.parameters.foo)。

预期:栏,但描述为未定义

我已经尝试过setTimeout()来评估将来的5秒,仍然无法正常工作,就像它根本没有加载一样。

        <noscript>
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="${width}" height="${height}" id="${application}">
            <param name="movie" value="${swf}.swf?foo=bar" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="${bgcolor}" />
            <param name="allowScriptAccess" value="sameDomain" />
            <param name="allowFullScreen" value="true" />
            <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="${swf}.swf?foo=bar" width="${width}" height="${height}">
                <param name="quality" value="high" />
                <param name="bgcolor" value="${bgcolor}" />
                <param name="allowScriptAccess" value="sameDomain" />
                <param name="allowFullScreen" value="true" />
            <!--<![endif]-->
            <!--[if gte IE 6]>-->
                <p> 
                    Either scripts and active content are not permitted to run or Adobe Flash Player version
                    ${version_major}.${version_minor}.${version_revision} or greater is not installed.
                </p>
            <!--<![endif]-->
                <a href="http://www.adobe.com/go/getflashplayer">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                </a>
            <!--[if !IE]>-->
            </object>
            <!--<![endif]-->
        </object>
    </noscript>   

AS3

package
{
import flash.display.LoaderInfo;
import flash.display.Sprite;

public class FlashVarsTest extends Sprite
{
    public function FlashVarsTest()
    {
        var paramsObj:Object =
            LoaderInfo(root.loaderInfo).parameters;
        trace("foo="+paramsObj["foo"]);
    }
}

}

这也不起作用:

package
{
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;

public class FlashVarsTest extends Sprite
{
    public function FlashVarsTest()
    {

        this.addEventListener(Event.ADDED_TO_STAGE, init)
    }

    private function init(event:Event):void
    {
        var paramsObj:Object =
            LoaderInfo(root.loaderInfo).parameters;
        trace("foo="+paramsObj["foo"]);

    }
}

}

1 个答案:

答案 0 :(得分:1)

看到OP代码后更新

您的错误位于非IE对象标记中:

 <object type="application/x-shockwave-flash" data="${swf}.swf?=foobar"

喜欢拼错!


您是否尝试过使用FlashVars对象参数标签而不是将其作为网址的一部分传递?

<object classid="blah blah">
    <param name="movie" value="test.swf" />
    <param name="FlashVars" value="foo=bar" />
    <embed src="test.swf" FlashVars="foo=bar" />
</object>

显然我在这里省略了很多额外的东西,但这应该说明变量传递是如何完成的。

作为旁注,通过URL传递变量应该在AS3中工作,正如Peter deHann的博客所示:http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html

测试自己:

我使用ExternalInterface输出到Firebug控制台。两个输出都按预期出现,证明没有与root.loaderInfo

相关的绘制延迟
package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.external.ExternalInterface;

    public class Main extends Sprite
    {
        public function Main()
        {
            if(ExternalInterface.available) ExternalInterface.call("console.log",root.loaderInfo.parameters.foo);
            (stage) ? init() : addEventListener(Event.ADDED_TO_STAGE,init);
        }

        private function init(evt:Event = null):void
        {
            if(evt) removeEventListener(Event.ADDED_TO_STAGE,init);
            if(ExternalInterface.available) ExternalInterface.call("console.log",root.loaderInfo.parameters.foo);
        }
    }
}

HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
    <head>
        <title></title>        
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <style type="text/css" media="screen"> 
            html, body  { height:100%; }
            body { margin:0; padding:0; overflow:auto; text-align:center; 
                   background-color: #ffffff; }   
            object:focus { outline:none; }
            #flashContent { display:none; }
        </style>          

    </head>
    <body>
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="Main">
                <param name="movie" value="Main.swf?foo=bar" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="allowScriptAccess" value="sameDomain" />
                <param name="allowFullScreen" value="true" />
                <!--[if !IE]>-->
                <object type="application/x-shockwave-flash" data="Main.swf?foo=bar" width="100%" height="100%">
                    <param name="quality" value="high" />
                    <param name="bgcolor" value="#ffffff" />
                    <param name="allowScriptAccess" value="sameDomain" />
                    <param name="allowFullScreen" value="true" />
                <!--<![endif]-->
                <!--[if gte IE 6]>-->
                    <p> 
                        Either scripts and active content are not permitted to run or Adobe Flash Player version
                        10.2.0 or greater is not installed.
                    </p>
                <!--<![endif]-->
                    <a href="http://www.adobe.com/go/getflashplayer">
                        <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                    </a>
                <!--[if !IE]>-->
                </object>
                <!--<![endif]-->
            </object>    
   </body>
</html>