问题描述
|
你好,我是新手。我只是想如何将值从Flash变量传递给PHP
我正在使用此代码
var myVars:LoadVars = new LoadVars();
myVars.playerName = \"Some Body\";
myVars.playerTime = Timer;
myVars.send(\"index.html\",\"_parent\",\"POST\");`
它显示错误->
警告:1060:迁移问题:不再支持LoadVars方法。有关更多信息,请参见urlvariables类,URLRequest.urlvariables和URLRequest.postData属性以及URLLoader.dataFormat属性。
请指导我如何解决此错误。我将CS4用于Flash和AS3.0
解决方法
LoadVars()
仅是AS2。您需要使用URLLoader
。试试这个课程:
package
{
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.Event;
/**
* @author Marty Wallace
* @version 1.00
*/
public class PHPData extends Object
{
/**
* Sends data to a PHP script
* @param script A URL to the PHP script
*/
public function send(script:String,vars:URLVariables):void
{
var req:URLRequest = new URLRequest(script);
req.data = vars;
req.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.load(req);
// listeners
loader.addEventListener(Event.COMPLETE,_complete);
}
/**
* Called when a response has been received from a PHP script
* @param e Event.COMPLETE
*/
private function _complete(e:Event):void
{
var vars:URLVariables = new URLVariables(e.target.data);
var i:String;
for(i in vars)
{
trace(i + \": \" + vars[i]);
}
e.target.removeEventListener(Event.COMPLETE,_complete);
}
}
}
然后,您可以按照以下方式进行操作:
var php:PHPData = new PHPData();
var vars:URLVariables = new URLVariables();
vars.playerName = \"Some Body\";
vars.playerTime = Timer;
php.send(\"index.php\",vars);
我注意到的另一件事是您正在使用send将数据发送到.html文档而不是.php文档。
有关创建要使用的此类的小教程(基于注释):
单击文件->新建-> ActionScript文件。
将上面的包(第一批代码)粘贴到新文件中。
将文件与.fla文件保存在同一目录中。
将第二段代码粘贴到.fla文件的时间轴中。
所有人都应该从这里开始工作。
这是一个.zip,其中包含您可以使用的示例。
http://junk.projectavian.com?f=phpdata.zip