PDA

View Full Version : SWFObject and AS3 Question


tptompkins
05-07-2007, 08:08 PM
Hello,

I'm trying to pass in a variable using the SWFObject so.addVariable("variable3", "value3"); and in the documentation it says:

"Just access them as you would any variable on the _root timeline."

So for my dumb question - how do you access a variable on the _root timeline with AS3 from within a function of a class?

I'm feeling stupid.

Tommy

springframework
05-07-2007, 08:10 PM
usually you would want to pass this variable in when creating the object.
or using a setter/getter, or by having a public variable.


since root is part of the displaylist you can access it by finding it using .parent (this is the worst way to access it).


variable3 is what it will be called


<a href="http://www.jessecouch.com">actionscript 3.0 vancouver</a> (http://www.jessecouch.com)

tptompkins
05-07-2007, 08:13 PM
When I do that I get this:

ReferenceError: Error #1065: Variable rc_domain is not defined.

"rc_domain" is the variable that I'm passing in this case. Any other ideas?

Thanks,

Tommy

dr_zeus
05-07-2007, 08:22 PM
I believe this should work:

root.loaderInfo.parameters.yourVariableName

springframework
05-07-2007, 08:27 PM
i didnt read your post properly.


usually you would want to pass this variable in when creating the object.
or using a setter/getter, or by having a public variable.


since root is part of the displaylist you can access it by finding it using .parent (this is the worst way to access it).


variable3 is what it will be called


<a href="http://www.jessecouch.com">actionscript 3.0 vancouver</a>

tptompkins
05-07-2007, 08:28 PM
Still not working...that gives me this:

ReferenceError: Error #1065: Variable root is not defined.

Not to deviate from the original question but how can I "create" a variable on the root timeline from within a class in AS3?

dr_zeus
05-07-2007, 08:35 PM
You need to access root from a display object (a MovieClip, a Sprite, etc). You also need to wait until the DisplayObject has been added to the stage before root becomes available. You'll probably want to listen to Event.ADDED_TO_STAGE to be sure you can access it.

tptompkins
05-07-2007, 09:33 PM
Forgive my ignorance but Flash / Actionscript is still new to me. I still can't figure this out. Basically I have an empty .fla file and created a class called "Main" and attached this class to the .fla file by specifying the "Document Class" textbox as "Main" in the .fla file using Flash CS3. When I test the .fla file it finds my Main class and executes it.

What I want to do is pass in the domain that the swf file is being served from using this:


var so = new SWFObject("rcgallery.swf", "rcmovie", "100%", "100%", "8", "#ffffff");
so.addParam("allowScriptAccess", "sameDomain");
so.addParam("quality", "best");
so.addParam("menu", "false");
so.addParam("scale", "noscale");
so.addVariable("rc_domain", "<?php echo $_SERVER["HTTP_HOST"];?>");
so.write("flashcontent");


All I want to do at this point is access the "rc_domain" variable from my constructor in my Main class. Here's my Main class:


package
{
import flash.display.Sprite;
import example.*;
import mx.controls.Alert;

/**
* Main Class
* @ purpose: Document class for movie
*/
public class Main extends Sprite
{
public function Main()
{
trace ("RC Domain: " + rc_domain);
}
}
}


But when I try this I get the following error: ReferenceError: Error #1065: Variable rc_domain is not defined.

I don't have any graphics or objects on the stage at this point. Any idea what I'm doing wrong? Basically I want to setup something that allows me to pass in some constants using SWFObject from my index.php file and then have access to those constants throughout my entire flash app. Is there a common way for doing this sort of thing?

Thanks,

Tommy

springframework
05-07-2007, 09:40 PM
ok in the actionscript part of the .fla file you can access your variable

rc_domain


so in the .fla file you can go

var my_var:Main = new Main(rc_domain);


and for the Main class make it like this.

public class Main extends Sprite
{
public function Main(my_domain:String)
{
trace ("RC Domain: " + my_domain);
}
}



that should work to get the variable rc_domain to you Main class object.


<a href="http://www.jessecouch.com">actionscript 3.0 vancouver</a> (http://www.jessecouch.com)

tptompkins
05-07-2007, 09:44 PM
When you say "the actionscript part of the .fla file" are you referring to adding actionscript to the first frame of the timeline? I also tried doing this and get this error when adding any actionscript to a keyframe:




ReferenceError: Error #1065: Variable addFrameScript is not defined.
at Main$iinit()[C:\Web Projects\RedCart2.0\RedCart\v3\rcFlash\actionscrip t\Main.as:1]

springframework
05-07-2007, 09:59 PM
yeah i mean the first frame of the .fla file

you must also

import Main;


either have the .fla file and Main.as in the folder or make sure Main.as is in the classpath

tptompkins
05-07-2007, 10:09 PM
What I've found is that if I add this to the first frame of the fla file, including the import statement, then I get this error:



ReferenceError: Error #1065: Variable addFrameScript is not defined.



But I noticed that if Main extends "MovieClip" and not "Sprite" then this error goes away but then I'm back to my previous error where rc_domain is still undefined.

By the way - I thought with Actionscript 3.0 putting script in frames was a no-no :confused: Is this true?

springframework
05-07-2007, 10:34 PM
yes, you can't add actionscript to anything other then the first frame of the root timeline


this is where you initialize your project ... i.e. read in data/create objects


it sounds like to me you have a variable "addFrameScript" that isnt defined anywhere.

<a href="http://www.jessecouch.com">actionscript 3.0 vancouver</a> (http://www.jessecouch.com)

tptompkins
05-07-2007, 11:29 PM
According to what I've read, you're also not supposed to put actionscript in the "first" frame if you're using AS3. When I attempt to do this, it yells at me with the "ReferenceError: Error #1065: Variable addFrameScript is not defined." error. It appears that you're supposed to associate a "document class" with the .fla file (which I defined as my Main class) and this class automatically gets instantiated when the file is run. This tells me that your projects actionscript should be kicked off and started via the constructor of this main document class:

-- From Flash CS3 Help --
"When you use ActionScript 3.0, a SWF file may have a top-level class associated with it. This class is called the document class. When the SWF is loaded by Flash Player, an instance of this class is created to be the SWF file's top-level object."

-- From Actionscript 3.0 Cookbook --
"In actionscript 1.0 and 2.0, you had many choices as to where to place your code; on the timeline, on buttons and movie clips, on the timeline of movie clips, in external .as files referenced with #include, or as external class files. Actionscript 3.0 is completely class-based, so all code must be placed in methods of your project's classes."

So if the document class automatically gets instantiated when the file is run then why would I instantiate Main using:

var my_var:Main = new Main(rc_domain);

in the first frame of the timeline (even if it let me - which it doesn't)? I'm just trying to understand!

Thanks!

Tommy

springframework
05-07-2007, 11:44 PM
"document class"

this is what im talking about.

create your Main class object in the document class.

tptompkins
05-07-2007, 11:53 PM
I apologize for not being clear, but I did create my Main class as the Document Class. When I run the file it executes my class perfectly fine and executes the constructor. I also got it to use AMFPHP Flash Remoting to talk to my server and hit a MySQL database, all without a hitch.

The only thing that I still can't figure out is how I can access variables that were passed/defined when using SWFObject to embed the swf file. I want to access these variables from the constructor of my Main Document Class. Does the variable feature of SWFObject even work with AS3??? I guess that should have been my first question. Make sense?

Thanks,

Tommy

tptompkins
05-08-2007, 12:50 AM
I believe this should work:

root.loaderInfo.parameters.yourVariableName

THANK YOU DR_ZEUS!

I went back to this and tried it again - got the same error. Then I emptied my browser cache, refreshed and low and behold - it works! So my stupid browser was caching the swf file and I wasn't seeing my changes. How can I prevent the browser from caching the swf file?

Thanks again -

Tommy

Azteroth
05-08-2007, 04:13 AM
Ok just a thought but you could try and put this at the top of the page before the <html> part and change the extension to the page your swf is on to .php


<?
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>


hope that helps,:)
Azteroth

tptompkins
05-08-2007, 02:11 PM
Awesome. I'll give that a try. Thank you!

Tommy

dr_zeus
05-08-2007, 07:02 PM
If you just need to clear the cache for testing, many browsers (at least Firefox and IE that I know of for sure) will fully reload a page when you press Shift while clicking the reload button.

tptompkins
05-08-2007, 07:19 PM
That's also a great tip! Thanks for sharing!

Tommy

tptompkins
05-08-2007, 08:00 PM
Hmmm...I'm still seeing the results of browser caching even through I've implemented both suggestions below. I used shift-[click refresh button] and also shift-F5 but it's still caching my swf. Any other suggestions?

You'd think that this would be a major problem with anyone trying to code/test a flash app and there's surely some standard to get passed this??

Thanks,

Tommy

springframework
05-08-2007, 10:02 PM
a trick you can do, which is most useful when loading data within a flash application, to make sure the user doesn't load old cached data is to add parameters to the html address or swf address:

data.php?id=234987 (<< some random number)
data.swf?id=998398327 (<< some random number)

that forces (i believe) all browsers to see its not cached since it hasnt ever seen that address before.


so when your looking at your website in the browser and can jsut at this to teh end of your addresses.

?something=randomnumbers313212323987972




and for the browser you can always just clear the cache or set the cache space to 0mb or set up cache to not cache anything. The options are different for every browser.

clearing all private data will also clear the session data which is usually a good thing for testing purposes.

<a href="http://www.jessecouch.com">actionscript 3.0 vancouver</a> (http://www.jessecouch.com)

Big-O
02-05-2008, 09:29 PM
I believe this should work:

root.loaderInfo.parameters.yourVariableName

Thank You!

panel
02-06-2008, 02:25 PM
If you just need to clear the cache for testing, many browsers (at least Firefox and IE that I know of for sure) will fully reload a page when you press Shift while clicking the reload button.

Also
IE, Firefox Crtl+F5 (load whole page again)
FireFox Shift+Crtl+Del - delete cache (you can disable showing confirmation windows in settings)