开发者

FlashVars w/o SWFObject doesn't work for pure AS3 project

This has been driving me nuts. I've worked with SWFObject in the past which is great. However I have a requirement not to use JavaScript. So when I try to do flashvars examples all over the net, they don't seem to work for me.

Steps to repeat:

1) Create a pure AS3 project using Flex or Flash Builder

2) In the index.html wherever there is a .swf, add a name value pair suffix. test.swf?foo=bar

3) In the con开发者_JAVA技巧structor of the main class Sprite, trace(root.loaderInfo.parameters.foo).

Expected: bar but traces out as undefined

I've tried setTimeout() to evaluate 5 seconds in the future, still doesn't work as if it's not loaded at all.

        <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"]);
    }
}

}

This doesn't work either:

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"]);

    }
}

}


Update after seeing OP code:

Your error is in the non-IE object tag:

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

Gotta love a typo!


Have you tried using the FlashVars object param tag instead of passing them as part of the URL?

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

Obviously I've omitted a lot of extra stuff here, but this should illustrate how variable passing is done.

As a side note though, passing variables though the URL should work in AS3, as this blog by Peter deHann illustrates: http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html

Testing myself:

I used ExternalInterface to output to the Firebug console. Both outputs come out as expected, proving that there is not drawing delay associated with 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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜