Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

howto create a session variable in flash/actionscript

I have an .fla with various actionscript 3 modules. I'm used to jsp and there is a concept of session variables. actionscript also appears to have session variables, but when I try to set one using:

SessionVar.Set("%navButton%", "about");

I get the error: 1120: Acccess of undefined property SessionVar.

What am I doing wrong?
Avatar of CyanBlue
CyanBlue
Flag of United States of America image

There is no such thing as a session variable in ActionScript...  

Maybe you can explain what you are trying to do and we can help you better...

CyanBlue
Avatar of Mark
Mark

ASKER

yes, I just figured that out. I was looking at some other post that had the work actionscript in it. Wrong.

In doing more research, I find I want a global variable which apparently must be defined using a new class, e.g.:

package com.greenethumb.utils
{
    public class GlobalVarContainer
    {
        public static var vars:Object = {};
    }
}

Now, I have just been adding AS code to event, etc. I have not had need to create any new classes. I don't know how to do this in AS3. If you could help me get going, I'm sure I could reference the variables from there. First of all, where do I create such a "package"?
Avatar of Mark

ASKER

On the other hand, if that idea is too complex, ... I have a movieclip actionscript that executes whenever any of 4 buttons is clicked. Is there a way of know which button triggered the clip?
Avatar of Mark

ASKER

Here's what I've tried so far. I created an actionscript file, buffaloGlobals.as in the com folder of my flash root. It contains the following:

package com.buffaloGlobals
{
    public class GlobalVarContainer
    {
        public static var vars:Object = {};
    }
}

I then put the following in the place where I want to put something in to my global container:

import com.buffaloGlobals;
GlobalVarContainer.vars.groupingID  = 1;

This gives me the error: Access of undefined property GlobalVarContainer

I should point out that I am trying to follow the instruction in: http://greenethumb.com/article/11/global-variables-in-as3
Hi there.  If you want to be able to add properties to a class on the fly, which can be useful in a case like this, then your class needs to be dynamic. So:

package com.buffaloGlobals
{
    public dynamic class GlobalVarContainer
    {
        ...
    }
}

Will allow yout to add whatever you like to GlobalVarContainer so you can use it as a place to store your 'session variables'.  You need to be careful using this kind of thing though - in general don't do it because of course the compiler will stop checking the validity of your property names.  Also note that you can only add public properties and methods to a dynamic class!

Hope this helps.
Avatar of Mark

ASKER

I've changed the .as file as you've recommended in 28667897. It is now:

package com.buffaloGlobals
{
    public dynamic class GlobalVarContainer
    {
        public static var vars:Object = {};
    }
}

The action script contents are the same:

import com.buffaloGlobals.GlobalVarContainer;
GlobalVarContainer.vars.groupingID  = 1;

Now I get the compile error:

1172: Definition com.buffaloGlobals:GlobalVarContainer could not be found.
How did the session variable question become something of a class question???  :D

jmarkfoley...  Can you upload the tester file???  It's fine if you want to close this topic and concentrate on the other topic at the AS.org...  Up to you...

CyanBlue
Avatar of Mark

ASKER

I'm new to actionscript. All I'm trying to do is create a global storage place of some kind. The code I've posted is just something I got off the web. My needs are simple: When the user clicks a button. I want to set a variable accessible by other actions that get run in the timeline. That's it. I have AS event handlers triggered by button clicks. I'd like to have that event handler set a variable so that other actions in the timeline know which button was pressed.

That's what I need. So far, my experimentation with this public package isn't working. In fact, it could be simpler than the example I've been trying. I just need a single integer or string with a variable name. Pseudo coding it in VB syntax I'd say:

public myButtonClick as String
:
myButtonClick = "gallery1"
:
if myButtonClick = "gallery1" then ...

that's it. That's all I need. How do I do that in AS?
Here is the quickest example I can come up with...

CyanBlue
GlobalVars.zip
Avatar of Mark

ASKER

I found the problem. The problem was in the whole folder structure. As I mentioned in my 28651900 post, I created the file buffaloGlobals.as in the com folder as:

package com.buffaloGlobals
{
    public dynamic class GlobalVarContainer
    {
        public static var vars:Object = {};
    }
}

I then put the following lines in my action script:

import com.buffaloGlobals.GlobalVarContainer;
GlobalVarContainer.vars.groupingID  = 1;

That gave me an error, as I stated. BUT, when I changed the file name from buffaloGlobals.as to GlobalVarContainer.as and put that in the folder: com/buffaloGlobals/ it started working.

This was all a matter of monkey-at-the-typewriter experimentation. I have no real idea why this works from the formal language definition point of view. If someone could give me an explanation, or point me to an authoratative link explaining the correlation between class/package and directory hierarchy, I would be most grateful and award points.
ASKER CERTIFIED SOLUTION
Avatar of Deasil
Deasil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark

ASKER

OK, thanks! That is all very useful!!!