很抱歉这个标题让人困惑,我不知道该怎么说。我一直在跟随一个交互式动态flash Actionscript 3.0游戏的教程,该游戏与php和MySQL通信,以记住每个用户的某些信息。它首先向php文件getessionvars .php发送一个请求,该文件返回flash游戏可以使用的值来检索用户信息。基本上这里是所有重要的代码,从actionscript开始:
stop();
// Assign a variable name for our URLVariables object
var variables:URLVariables = new URLVariables();
// Build the varSend variable
// Be sure you place the proper location reference to your PHP config file here
var varSend:URLRequest = new URLRequest("getsessionvars.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
// Build the varLoader variable
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
variables.myRequest = "bringit";
// Send the data to the php file
varLoader.load(varSend);
// When the data comes back from PHP we access it here
function completeHandler(event:Event):void{
var idVar = event.target.data.id_var;
var userNameVar = event.target.data.uname_var;
var passVar = event.target.data.upass_var;
var resultStatus = event.target.data.my_result;
var coordX=event.target.data.coordx;
var coordY=event.target.data.coordy;
if (resultStatus == "no_session"){
gotoAndStop("no_session");
} else if (resultStatus == "no_exist"){
gotoAndStop("no_exist");
} else if (resultStatus == "all_good"){
userid_txt.text = idVar;
username_txt.text = userNameVar;
password_txt.text = passVar;
gotoAndStop(5);
var other:otherPeople = new otherPeople();
addChild(other);
other.x=coordX;
other.y=coordY;
}
}
然后返回getsessionvars.php:
<?php
session_start();
include_once("php_includes/check_login_status.php");
$id = ""; // Initialize $id var
$username = ""; // Initialize $username var
$password = ""; // Initialize $password var
if (isset($_POST['myRequest']) && $_POST['myRequest'] == "bringit"){
$id = preg_replace('#[^0-9]#i', '', $log_id);
$username = preg_replace('#[^a-z0-9]#i', '', $log_username);
$password = preg_replace('#[^a-z0-9]#i', '', $log_password);
// Check database to see if the id is related to this password
include_once("connect.php");
mysql_query("INSERT INTO online ('id','player','xpos','ypos') VALUES('','{$username}','10','30')");
$sql = mysql_query("SELECT * FROM users WHERE id='$id' AND username='$username' LIMIT 1");
$numrows = mysql_num_rows($sql);
$sqla=mysql_query("SELECT * FROM online");
echo "my_result=all_good&id_var=$id&uname_var=$username&upass_var=$password&coordx=30&coordy=50";
}// close inital if condition
?>
我的问题是:我怎样才能使它,使多个用户可以出现在屏幕上在同一时间?正如你所注意到的,我已经尝试着在玩家第一次登录MySQL数据库时存储他们的坐标,然后希望每次角色移动时更新这些信息,但我想知道是否有更有效的方法来做到这一点?
除非你的游戏速度较慢且基于回合制,否则你就完全走上了错误的道路。同时多人游戏所需要的是插座服务器,如SmartFox服务器或ElectroTank服务器。
electro tank的人就这个话题写了一本非常好的书:http://www.amazon.com/ActionScript-Multiplayer-Games-Virtual-Worlds/dp/0321643364
你应该把这本书拿来读一读。它涵盖了设置服务器以及如何使您的flash实现与驱动更新的服务器一起工作。