Reputation: 3828
I am wondering if I can call a PHP script from AS3 and have it recognize only the vars that are echoed out. For example.
My PHP Script called GetMyVars.php
<?
Some code...
more code..
// I want to get these vars below into flash as3
echo "$MyVars"
echo "$MoreVars"
?>
Flash AS3 Script Calling GetMyVars.php
var fl_TextLoader:URLLoader = new URLLoader();
var fl_TextURLRequest:URLRequest = new URLRequest("GetMyVars.php");
fl_TextLoader.addEventListener(Event.COMPLETE, fl_CompleteHandler);
function fl_CompleteHandler(event:Event):void
{
var textData:String = new String(fl_TextLoader.data);
trace(fl_TextLoader.MyVars); /// I want this var in Flash
trace(fl_TextLoader.MoreVars); /// and this one too. :)
}
fl_TextLoader.load(fl_TextURLRequest);
Or what is the best way to do this. In short what I want to do is have a folder full of images. I want php to read those images and break them into arrays. Then I want to be able to have flash get the file names into an array and load the images fading on top of each other. Maybe there is an easier way to do this. Thanks for your kind help.
Upvotes: 1
Views: 3268
Reputation:
I did this a while ago too. It's actually pretty easy. Just use &
and =
in your output, and AS3 will pick it up. It's abbit like GET vars in a URL.
So in your PHP:
echo "var1=fooooo&var2=barrrr";
Now, in your flash:
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,onLoaded);
loader.load(new URLRequest("your-script.php"));
var variables:URLVariables = new URLVariables();
function onLoaded(evt:Event):void
{
var data:URLVariables = new URLVariables(evt.target.data);
trace data.var1;// access var1 with data.var1
trace data.var2;//same with var2 (data.var2)
}
To grab all images in a specific directory, you can use glob()
. glob()
will return a assoc array of all the file names of the images in a specific directory.
So lets say you have a load of jpg
images in the folder bob
:
$imageNames = glob('bob/*.jpg');//you're probably wondering what the * is...it's a wildcard (which basically means any letter/number)
//define end var (the var that will contain the end content to print)
$flash_readMeNOW = '';
//define counter
$i = 0;
//now loop through that array and append it to a var
foreach ( $imageNames as $name )
{
//url-encode the name
$name = urlencode($name);
//increment i
$i++;
//append name with incremented number
$flash_readMeNOW .= '&imageName'.$i.'='.$name;
}
//and now print out the data
echo $flash_readMeNOW;
Now use the flash code I provided above, and access all image names with data.imageName1
, data.imageName2
, data.imageName3
, ect ect...
Oooo one more thing, make sure you're not printing anything else out in this script. Why? Flash won't be able to pick it up for some reason. This is including invisible things such as white spaces and line breaks. SO if you're using the closing php tag (?>
), make sure there is no content below it (as that will then be printed out).
Upvotes: 0
Reputation: 17348
I would recommend outputting the content from PHP as an XML file, then parsing the XML within ActionScript.
PHP
//Output this file as XML (not really necessary)
header("Content-type: text/xml");
//Some code...
echo "<root>
<xmlNode1>" . $MyVars . "</xmlNode1>
<xmlNode2>" . $MoreVars . "</xmlNode2>
</root>";
ActionScript [From here]
var xmlString:URLRequest = new URLRequest("file.php");
var xmlLoader:URLLoader = new URLLoader(xmlString);
xmlLoader.addEventListener(Event.COMPLETE, init);
function init(event:Event):void{
var xDoc:XMLDocument = new XMLDocument();
xDoc.ignoreWhite = true;
var myXML:XML = xmlLoader.data as XML;
xDoc.parseXML(myXML.toXMLString());
trace(xDoc.xmlNode1); //Outputs whatever $MyVars equals
}
I haven't tested the above code, but it should work.
Hope that helps.
Upvotes: 1
Reputation: 13532
To get all the files in a directory on php :
<?php
$files = glob("yourdir/*.*");
echo json_encode($files);
?>
On AS3 side call that php file, it will give you a json encoded array of file names, then you can decode it to get the array and load the images :
var fl_TextLoader:URLLoader = new URLLoader();
var fl_TextURLRequest:URLRequest = new URLRequest("GetMyVars.php");
fl_TextLoader.addEventListener(Event.COMPLETE, fl_CompleteHandler);
function fl_CompleteHandler(event:Event):void
{
var textData:String = new String(fl_TextLoader.data);
var images:Array = JSON.decode(textData);
for each (var imageFileName:String in images)
{
loadImage(imageFileName);
}
}
function loadImage(imageFileName:String)
{
//image loading code
}
fl_TextLoader.load(fl_TextURLRequest);
Obviously I haven't tested this code but it should get you started.
Upvotes: 1