AS3 - Retrieving Flashvars values

Hi Experts,

Please bear with my terminology as I am not a flash guy, but I will do my best.

I am trying to pass values to my Flash movie using flashvars. My Flash movie is embedded into my test page using swfobject 2. I've tried a bunch of tutorials, and I think I'm on the right track, but I don't think I'm doing something quite right. The movie DOES work when I hard code the values into my flash movie instead of trying to pull them in, so I am certain that I am just not retrieving the values correctly.

Please check out my snippets of code below. Any help in the right direction is really appreciated.



Here's the HTML code that sets the variables and calls in the movie:
http://pastie.org/private/3tcn78wp4msaqadnflstea



Here is what I need the final output to be in the first frame of my FLA:
http://pastie.org/private/zsdi0hcauzkdydgzenfmw



Here is the code I am using to try and create the output above. I followed a few tutorials to get to this point. I have placed a text field on the stage with an instance name of output on the stage. The actions in the link below are on the first frame of my movie. I really appreciate your help in this.

http://pastie.org/private/pd4ri5kxx02dhna5nctv8a





CSS_GuyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IqAndreasCommented:
I'm having a bit of a difficult time understanding what you are trying to do with the FlashVars.

The first code is what you are using to embed the SWF, that I see, and that code looks fine to me.

The third code you provided should work by just placing it directly onto the frame where you want the flash variables. However, there is a lot of repetitious code, so I attached the code for a stripped down version with all that you need. I also placed trace statements in there to loop through all your variables so you can double check if the values are reaching Flash or not.

Try out the code, and tell me what results you get from the "trace".
//Place this on frame 1 of your timeline
var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;

//What is this "awesomemovie" object you are referring to?
awesomemovie.xmlSource = String(allFlashVars.xmlSource);
awesomemovie.cssSource = String(allFlashVars.cssSource);
awesomemovie.imageSource = String(allFlashVars.imageSource);


// The following part is not necessary, but will help you debug the problem
for (key:String in allFlashVars) {
   trace(key + " = " + String(allFlashVars[key]));
}

Open in new window

CSS_GuyAuthor Commented:
Hi,

Thank you very much for the code. Basically, I am just trying to store values in the flashvars, and then I need to pass those variables to my flash movie.

The second link I posted is how I need my final output to look, after the info has been pulled from the flashvars and outputted into the flash movie.


I inputted your code and got the following error:

1084: Syntax error: expecting in before colon.
CSS_GuyAuthor Commented:
sorry the other part of my poste didnt get posted....

the source for that error is below
for (key:String in allFlashVars) {

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

IqAndreasCommented:
Sorry about that error. I forgot Flash doesn't like typed keys in "for in" loops.

Just remove the ":String" part, and it should work. Fixed code attached.
//Place this on frame 1 of your timeline
var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;

//What is this "awesomemovie" object you are referring to?
awesomemovie.xmlSource = String(allFlashVars.xmlSource);
awesomemovie.cssSource = String(allFlashVars.cssSource);
awesomemovie.imageSource = String(allFlashVars.imageSource);


// The following part is not necessary, but will help you debug the problem
for (key in allFlashVars) {
   trace(key + " = " + String(allFlashVars[key]));
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CSS_GuyAuthor Commented:
Thank you so much. The trace was throwing back errors again, but I took that out, and the flashvars are being pulled correctly.

Thank you so much!!
Jakob_ECommented:
As a lazy dog I would  use a dynamic singleton class - this way you can access
the flashvars across all your imported classes

 
// Class:
package  whatever.global {
    import flash.utils.Dictionary;
    import  flash.display.LoaderInfo;
    dynamic public class FlashVars extends  Dictionary {
        private static var _instance : FlashVars = new  FlashVars(SingletonLock);
        public static function get  instance():FlashVars { return _instance; }
        public function  FlashVars(lock:Class):void { if(lock!= SingletonLock) throw new  Error(this + " Invalid Singleton access. Use FlashVars.instance.");}
         public function parse(info:*):void {
            var  info=(info is LoaderInfo) ?info.parameters : info;
             for(var p in info){ this[p]=unescape(info[p].toString()); Debug.log(p +  " : " +info[p]) }
        }
    }
}
internal class  SingletonLock {}




//  Usage:// Import the class
import whatever.global.FlashVars;

// Create a test object to act as flashvars when using the testplayer
var flashTestVars:Object={
    xmlSource:"awesomemovieXML.xml",
    cssSource:"awesomemovieCSS.css",
    imageSource:"images"
    };

// If the current movie is the root swf and hereby have access to flashvars
// Call the parse method of the singleton class (no instantiation)
// If you are running in the testplayer pass the flashTestVars - else pass loaderInfo
if (loaderInfo.url==loaderInfo.loaderURL) {
    FlashVars.instance.parse((Capabilities.playerType=="External") ? flashTestVars : loaderInfo);
}

// After having called the parse method you can access your
// flashvars by referencing the FlashVars singleton - like:
var flashvars:FlashVars=FlashVars.instance;
trace(flashvars.xmlSource);// returns "awesomemovieXML.xml"
trace(flashvars.cssSource);// returns "awesomemovieCSS.css"
trace(flashvars.imageSource);// returns "images"




Best,
Jakob E


Jakob_ECommented:
Just spotted an error - please remove the
Debug.log(p +  " : " +info[p])
...that was just part of my own tesing
IqAndreasCommented:
@Jakob_E
Just curious, why did you extend the Dictionary object instead of extending a traditional dynamic Object?
Dictionary objects are mainly for passing in objects as indexes rather than strings and numbers.

Still, handy class. I will look into it and may use it in the future. :)

Andreas
Jakob_ECommented:
Very good question indeed - I just made a quick re-write of an old class
when dealing with strings I agree that this is a bit overkill :-)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.