htmlactionscript-3flash-cs5flashvars

Send data from HTML to Actionscript3


I am trying to send username from HTML to ActionScript 3. And I referred this tutorial

HTML Code:

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>      
    <object width="100" height="100">
        <param name="movie" VALUE="test.swf">
        <param name="FlashVars" VALUE="userName=permadi">
        <embed src="test.swf" FlashVars="userName=permadi" width="1024" height="768">
        </embed>
    </object>
</body>
</html>

Actionscript3 Code:

 function loaderComplete(myEvent:Event)
{
  var flashVars=this.loaderInfo.parameters;
  userNameTextField.text=flashVars.userName;
}

var flashVars=new Object();
flashVars.loaderCOmplete:function=loaderComplete;
this.loaderInfo.addEventListener(Event.COMPLETE, flashVars.loaderComplete);

But userNameTextField in swf file does not displays userName value.


Solution

  • To get your flashvars, you can do like this :

    this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
    function loaderComplete(myEvent:Event)
    {
        var flashVars =  this.loaderInfo.parameters;
        // verify if your flash var is set
        if(flashVars.userName){
            userNameTextField.text = flashVars.userName;
        }
    }
    

    Or simply like this :

    // verify if your flash var is set and set a default value if not, if you want of course
    userNameTextField.text = this.loaderInfo.parameters.userName ? this.loaderInfo.parameters.userName : 'default value';
    

    And don't forget to embed your font as flash said : "Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.", you can see here how to embed fonts for a dynamic text field.