简体   繁体   English

Flash As3.0 PHP变量?

[英]Flash As3.0 PHP Variables?

Is it possible to make php variables public and accessible anywhere on the timeline? 是否可以使php变量在时间轴上的任何地方公开和访问?

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. 问题:当SWF第一次加载时,它将拉动信息并显示该信息,但是如果我移至第二帧然后再回退,则会擦除该信息。 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. 返回第一帧后查看变量的唯一方法是重新加载整个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;

        }
    }
}
}

Why not use the FlashVars tag/attribute? 为什么不使用FlashVars标签/属性?

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: 要使一系列PHP变量可用于Flash,请将它们组合成一个数组,并使用http_build_query创建flashvars参数:

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

// ...

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

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. 在.fla的第一帧中,创建此类的新实例,并且每个帧都可以导入该类并通过静态变量进行访问,另一种解决方案是也创建get和set函数。

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 希望这会有所帮助,干杯,威尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM