views:

388

answers:

2

Is it possible to make php variables public and accessible anywhere on the timeline?

The script I included works fine if I set it as the document class only (it won't work if I try importing it). The variables pass the text to dynamic text fields on the main timeline.

The problem: It will pull the information and display it when the SWF first loads but if I move to my second frame and then back it erases the information. It also will not pass any variables to the second frame. The only way to see the variables after going back to the first frame is to reload the whole SWF. I'm pretty much stuck at this point trying to make the variables persistent through all frames.

this is my code:

package {
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.display.Stage;

public class Main extends MovieClip
{
    public function Main() {

        var request:URLRequest = new URLRequest("http://localhost/mytest2/dataLayer.php");
        request.method = URLRequestMethod.GET;

        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        loader.addEventListener(Event.COMPLETE, completeHandler);
        loader.load(request);

        function completeHandler(evt:Event) {

            var username = evt.target.data.username;
            var pclass = evt.target.data.pclass;
            var hpoints = evt.target.data.hpoints;
            var spoints = evt.target.data.spoints;

            username_txt.text = username;
            class_txt.text = pclass;
            hpoints_txt.text = hpoints;
            spoints_txt.text = spoints;

        }
    }
}
}
A: 

Why not use the FlashVars tag/attribute?

To make a sequence of PHP variables available to Flash, combine them into an array and use http_build_query to create the flashvars param:

$vars = array('var_one' => $var_one, 'var_two' => $var_two);
$flashvars = http_build_query($vars);

// ...

echo "<PARAM NAME=FlashVars VALUE=\"$flashvars\">";
Josh
A: 

What you could do is create a seperate class with your varibles inside, for example:

  package
  {
      public static var username:String;
      public static var pclass:String;
      public static var hpoints:Number;
      public static var spoints:Number;
      public class my_globals
      {
          public function my_globals():void {
            var request:URLRequest = new URLRequest("http://localhost/mytest2/dataLayer.php");
            request.method = URLRequestMethod.GET;

            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.load(request);

          }

          private function completeHandler(evt:Event) {
            username = evt.target.data.username;
            pclass = evt.target.data.pclass;
            hpoints = evt.target.data.hpoints;
            spoints = evt.target.data.spoints;  
         }

      }
  } 

In the first frame of your .fla create a new instance of this class and each frame you can import the class and access through the static variables, another solution would be to create get and set functions too.

import my_globals;

var globals:my_globals = new my_globals();

username_txt.text = my_globals.username;
class_txt.text = my_globals.pclass;
hpoints_txt.text = my_globals.hpoints;
spoints_txt.text = my_globals.spoints;

Hope this helps, Cheers, Will

WillDonohoe
I still can't get this damn thing working. the public vars have to be inside the class. They can't be right after the package open. It seems like it isn't completing the function when I make it into a package. If I run it on the timeline or put it in my Document Class it runs perfect but as a stand alone package it will not do anything.It would be really great if I could keep it standalone and call the variables in my main Document Class.If I use username_txt.text = (my_globals.username); In my main document class it always pulls a null object. It will not pull the users name.