views:

66

answers:

1

Hello,

I'm really frustrated in this case.

While developing with Adobe Flex, I'm working on my first application - and use pretty much actionscript.

In my mxml application, I include as3 file via <mx:Script source="as/myas3file.as></mx:Script>.

In myas3file.as, I include (thru include "variables.as";) file variables.as, which contains following code:

var timer:Object = new Object();
timer.t = 60;

or (in other test case)

var timer:Object = {t:60, j:"80"};
timer.t = 80;

Neither case works! Even if I rewrite example code from official documentation, it throws an 1020 error. I'm banging my head to the table for last two hours and I can't figure out what I'm doing wrong.

Thank you

A: 

If the code is included from a <Script /> tag in an MXML application, then what you're defining is a member variable and you can't use statements. From the docs:

You use the <mx:Script> tag to insert an ActionScript block in an MXML file. ActionScript blocks can contain ActionScript functions and variable declarations used in MXML applications.

...

Statements and expressions are allowed only if they are wrapped in a function. In addition, you cannot define new classes or interfaces in blocks. Instead, you must place new classes or interfaces in separate AS files and import them.

Instead, you can use an initializer as in your second example:

private var name:Object = { field: 80 };

or you can do the initialization in a function (constructor, initialize/creation complete event handler).

Michael Brewer-Davis
thank you, i already wrapped all functions and objects in new class
Adam Kiss