I can pass my SWF file a variable from PHP using Flashvars, but when I try to pass in a number Flash tells me it's NaN. Is there something I can do to make sure it is a number? I'm not sure what to do here. Can someone help? Thanks in advance.

I can pass my SWF file a variable from PHP using Flashvars, but when I try to pass in a number Flash tells me it's NaN. Is there something I can do to make sure it is a number? I'm not sure what to do here. Can someone help? Thanks in advance.

Could you post the code you're using, both PHP and Flash/ActionScript.

I'm guessing here, but I don't think you can pass anything but strings in flashvars.

Note: this is based on my experience with FLEX. I'm sure it should work with Flash.

You should be able to cast it to an Int or Number.

var myINT:int = int(Application.application.parameters.integer);

or

var myNumber:Number = Number(Application.application.parameters.number);

You can also use externalInterface to get "talk" between flash and javascript.

eg;

In your Flash ActionScript:

<mx:Script>
<![CDATA[
var Config:Object = ExternalInterface.call("getConfig");
]]>
</mx:Script>

This instructs the SWF to call the JavaScript function getConfig() on the HTML page it is embedded in. The Object "Config" will then be assigned the return value of getConfig().

In HTML page JavaScript:

<script type="text/javascript">
<![CDATA[
function getConfig() {
  return {
    'primitive1': 'value1', // a string
    'primitive2': 2, // an int
    'obj1' : { } // empty object
  };
}
]]>
</script>

With this method, you can pass objects in addition to primitive types.

Okay, you're talking over my head. I don't know if it matters, but I'm using AS2. What do you mean by passing "objects and primitive types"? But let me see if I understand this: first, cast the number into an INT using javascript, then use the externalInterface to talk between Flash and Javascript, then tell Flash to call the getConfig function. Is that right? Will this work in AS2?

Okay, you're talking over my head. I don't know if it matters, but I'm using AS2. What do you mean by passing "objects and primitive types"? But let me see if I understand this: first, cast the number into an INT using javascript, then use the externalInterface to talk between Flash and Javascript, then tell Flash to call the getConfig function. Is that right? Will this work in AS2?

I've only used Flash since AS3. I'm sure some of the syntax would be different from AS2.

However, casting a type should be more or less the same.

The examples I have were for AS3 in Flex.

To cast in AS3 and maybe 2, you just use the function with the same name as the type you want to cast to.

For example, to make a variable a Number.

var number = Number(number);

That will make "number" a Number.

So when you receive a flashvar, cast it to the type you need to use in AS3.

Primitive types are numbers, strings, integer etc. The rest are usually referred to as Objects.

Thanks. I'll give it a try. I hope AS2 works like AS3. I'm trying to get out of AS2, but I have some old projects that I don't want to toss just yet.

This article has been dead for over six months. Start a new discussion instead.