0
<?php
    $someVar = count(scandir("bilder/") - 2);
?>

I need to import $someVar in to my .fla-file, I only need its value.:

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, nextSlide2);
timer.start();

var ld:Loader = new Loader();
var bildeArr = [];

var backend: String = 'backend.php';
var loader: URLLoader = new URLLoader;

loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load( new URLRequest(backend));

trace(loader.data);



ld.x = 20;
ld.y = 20;
for(var i:int = 1; i<9; i++) {
    bildeArr.push("bilde" + String(i));
}

i = 0;
function nextSlide2(evt:TimerEvent):void {
    trace(i);
    ld.load(new URLRequest("bilder/" + bildeArr[i] + ".png"));
    addChild(ld);
    if(i > 6) {
        i = -1;
    } 
    i++;
}

How?

1
  • Do you need it loaded in at the beginning of the script load, or is this something you need to call after the script has AS3 already started? Commented May 19, 2011 at 16:48

2 Answers 2

5
<?php
 echo 'someVar=' + htmlentities($someVar);
?>

Flash:

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, varsLoaded);
loader.load(new URLRequest("url_to_php_file.php")); 
var varToAppend:String = "";
function varsLoaded (event:Event):void {
     varToAppend = loader.data.someVar.toString();
}

I hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

LoadVars() is no longer supported in AS
sorry, I haven't seen AS3, sorry again.
2

You need to add a listener to the loader. It will call a function when the data has loaded.

the important thing to know is that loading data from a server is asynchronous. It does not happen immediately, and flash does not stop running while it is waiting to get the next data. So you need to make sure you do not call any code that requires the data before the data is loaded. This code will continue to call the loop at the bottom before the data has loaded. If you want to use the data in your loop, you will need to move that code into a function and call that function from the onDataReady function.

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, nextSlide2);
timer.start();

var ld:Loader = new Loader();
var bildeArr = [];

var backend: String = 'backend.php';
var loader: URLLoader = new URLLoader;

loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.Complete, onDataReady)
loader.load( new URLRequest(backend));

// Asynchronous call - will be called when the data is loaded.
function onDataReady(e:Event):void {
    trace(loader.data);
    // Once loaded call another functon to use your data.
}



ld.x = 20;
ld.y = 20;
for(var i:int = 1; i<9; i++) {
    bildeArr.push("bilde" + String(i));
}

i = 0;
function nextSlide2(evt:TimerEvent):void {
    trace(i);
    ld.load(new URLRequest("bilder/" + bildeArr[i] + ".png"));
    addChild(ld);
    if(i > 6) {
        i = -1;
    } 
    i++;
}

6 Comments

I can see that it works, but I'm still not sure how to get the value of the variable.
I'm sorry, but i just don't get how to get the value of the variable.
You should sun the program and look at the output of the trace(loader.data) line. It may be [object Object] or it may be a string. I am not certain what the server returns. XML? JSON? name-value pairs? What kind o return value are you expecting? Post here and I'll try and help out some more.
When i trace i get this: %3C%3Fphp%0A%09%24var2%20=%20count%28scandir%28%22bilder%2F%22%29%20%2D%202%29%3B%0A%3F%3E My php-file looks like this: <?php $var2 = count(scandir("bilder/") - 2); ?> So what i want is the number of files within the folder
OK - So that appears to be unprocessed PHP there. If you use the unescape() function in AS3 you'll see that jibberish translates to : <?php $var2 = count(scandir("bilder/") - 2)%3‌​B ?> Looks like your PHP script is not getting exected. To see it in the code change the trace to: trace(unescape(loader.data));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.