0

我有一个Flash游戏。我想通过 .php 将我的分数发送到数据库。我怎样才能做到这一点?我已经写了一些帖子,但我无法弄清楚如何。我应该在 actionscript 3 端和 php 端做什么?我的数据库名称是 my_db,我的变量是 _score。

4

2 回答 2

3

首先,您需要创建一个 URLLoader 并将要发送的变量存储到请求的标头中。加载 URL 并发送变量后,我们监听 COMPLETE 事件,以便我们可以将变量从 PHP 中获取回 Flash。

动作脚本 3 代码:

private function SendScore(score:int) {
    //Use URLVariables Class to store our Variables and send them to our URL
    var variables:URLVariables = new URLVariables();
    //you can create as many variables you want (variables.variablename);
    variables.score = score;
    //URLLoader to load the URL
    var urlloader:URLLoader = new URLLoader;
    //Simple URLRequest with our URL
    var urlrequest:URLRequest = new URLRequest('http://www.mysite.com');
    //We set the method to POST. You can also use GET
    urlrequest.method = URLRequestMethod.POST;
    //We declare our set of variables to the data of our urlrequest
    urlrequest.data = variables;

    //We load the URL           
    urlloader.load(urlrequest);
    //We need to listen to an Event.COMLETE when we want to load variables FROM PHP
    urlloader.addEventListener(Event.COMPLETE, CompleteHandler, false, 0, true);
    //With  the listening to IOErrorEvent.IO_ERRORwe can intercept an error and can use it for Debugging
    urlloader.addEventListener(IOErrorEvent.IO_ERROR , ErrorHandler, false, 0, true);
}

//CompleteHandler will be used when the Load of the URL is completed
private function CompleteHandler(e:Event) {
    //Received Variables from PHP script
    var vars:URLVariables = new URLVariables(e.target.data);
    //You can access all variables from PHP with vars.xxxx
    //Example: vars.var1, vars.var2
    if(vars.success) trace('Saving succeeded');
    else ('Saving failed');
}

//ErrorHandler to receive error messages and don't fire exception errors
private function ErrorHandler(e:IOErrorEvent) {
    trace('Error occured');
}

在 PHP 中,我们通过 Flash 的 POST 方法接收变量,并且可以将它们与 $_POST['xxx'] 一起使用。我们将分数插入数据库,然后检查 INSERT 是否成功,然后将此变量发送回“浏览器”/Flash。

PHP 代码(我使用 PDO 进行数据库操作: http: //php.net/manual/de/book.pdo.php):

try {
    //Establishing database connection with PDO
    $DB = new PDO($dsn, $user, $pw, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
} catch (PDOException $e) {
    //Error catching
    print "Connection to Database failed: " . $e->getMessage() . "<br />";
    die();
}

//Create a SQL statement, prepare it and execute it to INSERT the data into database
//the "variables.score" from AS3 can be read with $_POST['score'], cause we used POST as method
$sql = "INSERT INTO my_db (is_score) VALUES ('".$_POST['score']."');";
$stmt = $DB->prepare($sql);
$stmt->execute();

//Here we send back a variable to AS3:
//If you want to send more variables back use the & sign to append more variables
//example: 'succeds=true&var1=this&var2=there'
//Can be read in CompleteHandler with vars.success
if($stmt->rowCount() > 0) echo('success=true');
else echo('success=false');
于 2012-07-11T12:11:48.093 回答
0

最简单的方法是从 Flash 应用程序中调用 php Web 脚本,使用 HTTP GET 将新分数发送到 URL。使用 URLLoader。然后 PHP 脚本可以向 Flash 应用程序返回一个确认,表明一切正常。

这是一个关于如何使用 URLLoader 的示例,简单的 PHP 知识就足够了。

于 2012-07-11T11:57:44.667 回答