00:00
00:00
Newgrounds Background Image Theme

lbkush just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Load External Data/cross-domain

6,426 Views | 14 Replies
New Topic Respond to this Topic

AS: Main

Loading external data

===================================================================

Pulling data from a local file using loadVars

Create a text file in notepad, and type something in it. Name it 'wah.txt' and place it in the same directory as your fla

Create a dynamic text box with the var: RADISH. This will hold the loaded data.
loadVars in its simplest form - add this code to the first frame of your timeline:

PIE=new LoadVars();
PIE.onData=function(raw){
RADISH=raw;
}

This will load the contents of your text file and display them in the textbox. To load the contents using a button, add this to your button:

on(press){_root.PIE.load("wah.txt");}

You can manipulate RADISH as you would any other string, once the load has finished.

===================================================================

Pulling data from a remote file using loadVars

NOTE: Some of these codes will NOT work when you preview the swf by pressing CTRL+ENTER.
If you get a 'Error opening URL' or 'undefined' values, publish the swf and then view it in the standalone player or a browser.

The limitations of accessing only local files are obvious. To achieve the full potential, you'll probably want to access a data file on the net.

- To access a remote file from your PC, just change the file path in the button actions:

on(press){_root.PIE.load("http://Domain_A.com/subfolder/wah.txt");}

- If your swf is uploaded and the "wah.txt" file is on the same server, use a relative path as you would on your machine

on(press){_root.PIE.load(../subfolder/wah.txt");} or on(press){_root.PIE.load("wah.txt");}

- NOW if your swf is uploaded (to Domain_A), but your data file is on a different domain (Domain_B), we hit complications (AKA Flash's Security Sandbox)

Firstly, you'll need to use an absolute path on your button. Then you'll need a System.security.allowDomain command.

on(press){
System.security.allowDomain("Domain_B.com")
_root.PIE.load("http://Domain_B.com/subfolder/wah.txt");
}

...but wait, it still doesn't work!!! WTF!?!?! (well, those were the sort of words I used when I first tried this)
So, apparently, we need a crossdomain file on Domain_B (the one that hosts 'wah.txt'). Open Notepad, and paste this in:

<?xml version="1.0"?>
<!-- http://www.Domain_B.com/crossdomain.xml -->
<cross-domain-policy>
<allow-access-from domain="*.Domain_A.com" />
</cross-domain-policy>

Save this file as crossdomain.xml (it must be named this), and upload it to the ROOT of Domain_B.
BINGO! Now you can host a swf on NG, and pull data (php, html, text, asp, etc) from your own site.

===================================================================

I'm aware that many people will want to send data as well as recieve it. Since I'm trying to keep the AS: threads easily readable, that will be covered in a separate topic (when I or someone else gets around to it).

Associated reading:

http://www.sitepoint.com/forums/showthread.php?t=120122&mode=linear
http://www.actionscript.org/forums/archive/index.php3/t-58351.html
http://livedocs.macromedia.com...MX_2004&file=00001418.html
http://www.moock.org/asdg/technotes/crossDomainPolicyFiles/


- - Flash - Music - Images - -

BBS Signature

Response to As: Load External Data/cross-domain 2005-07-01 17:15:41


Nice. this will help solving issues with this kind of programming.

~gorman2001


website :: hugostonge.com

my job :: we+are

Response to As: Load External Data/cross-domain 2005-07-01 17:20:01


I was going to say "Cool,nice work Denvish!" When I just realised that I always say that.
Should I come up with something more original?

"I didn't read your post at all since I am lazy and tired.I am going to say good work because I guess your script works.I actually don't care about this thread right now,I am not going to read it,I am going to go to bed.Anyway,I guesss that tutorial is good."

"Cool,nice job,Denvish"


BBS Signature

Response to As: Load External Data/cross-domain 2005-07-01 17:51:46


At 7/1/05 05:20 PM, -Toast- wrote: "I didn't read your post at all since I am lazy and tired.I am going to say good work because I guess your script works.I actually don't care about this thread right now,I am not going to read it,I am going to go to bed.Anyway,I guesss that tutorial is good."

lol, thanks I guess. Sleep well...


- - Flash - Music - Images - -

BBS Signature

Response to As: Load External Data/cross-domain 2005-07-01 17:58:43


Now I have read it XD
It wasn't bad at all!
I don't think I will use it,but it's always god to have it bookmarked ; )


BBS Signature

Response to As: Load External Data/cross-domain 2005-07-02 07:17:45


very useful and informative :) especially the cross domain part

Response to As: Load External Data/cross-domain 2005-07-28 18:05:18


I heard rumours about some html comptability starting to come into flash with this feature... is this true? and/or would it involve a stupid amount of programming?

Response to As: Load External Data/cross-domain 2008-07-09 05:16:33


I need to be able to load only certain parts of the .txt file, is that possible?

I need to store a lot of different variables in the .txt file, and they draw them into the .swf, how would I go about that?


BBS Signature

Response to As: Load External Data/cross-domain 2008-07-09 05:18:35


At 7/9/08 05:16 AM, Hoeloe wrote: I need to be able to load only certain parts of the .txt file, is that possible?

I need to store a lot of different variables in the .txt file, and they draw them into the .swf, how would I go about that?

You cannot read just a bit of the file. May I suggest using a php file and passing it the name of the variable and it just outputs that single variable


- Matt, Rustyarcade.com

Response to As: Load External Data/cross-domain 2008-07-09 05:28:43


Haven't spoken to you in a while, NinjaChicken... But thanks, I'll give that a shot!


BBS Signature

Response to As: Load External Data/cross-domain 2008-07-09 05:55:23


At 7/9/08 05:28 AM, Hoeloe wrote: Haven't spoken to you in a while, NinjaChicken... But thanks, I'll give that a shot!

Just to give you a hand:

<?php
$var = $_POST['var'];
$a = "Hello";
$b = "World";
echo "ret=".${$var};
?>

That should work fine and dandy.
So pass the script a var name with sendAndLoad, and the result will always be in ret.


- Matt, Rustyarcade.com

Response to As: Load External Data/cross-domain 2008-07-09 05:56:23


Sorry for double post, but if ${$var} doesnt work then try $$var instead


- Matt, Rustyarcade.com

Response to As: Load External Data/cross-domain 2008-07-09 05:58:30


At 7/9/08 05:55 AM, Rustygames wrote:
At 7/9/08 05:28 AM, Hoeloe wrote: Haven't spoken to you in a while, NinjaChicken... But thanks, I'll give that a shot!
Just to give you a hand:

<?php
$var = $_POST['var'];
$a = "Hello";
$b = "World";
echo "ret=".${$var};
?>

That should work fine and dandy.
So pass the script a var name with sendAndLoad, and the result will always be in ret.

I'm really kinda new at external data... I only ever worked with cookies before, and never with php... I think I kinda get the gist, but just to be clear, I need to store hundreds of strings in an external file, then draw, say 50 of them at random into an array in the flash file. Is that possible?


BBS Signature

Response to As: Load External Data/cross-domain 2008-07-09 06:01:38


Yes, do it like that, or use an external DB. Since you're so new I would do it like that first and later upgrade to a DB if needs be. This is more of a question of PHP, not flash


- Matt, Rustyarcade.com

Response to As: Load External Data/cross-domain 2009-11-09 08:51:23


sorry for the bump, is it possible to CREATE text files within the parent directory, with a given name?


Awesome turret game in the making, featuring 85 uniqe turrets: News post

BBS Signature