简体   繁体   English

使用Ajax更新游戏项目数据库

[英]Game project updating database using ajax

Im trying to make a realtime game with no page refresh. 我正在尝试制作没有页面刷新的实时游戏。 The problem is that i dont know what to do next or how to configure ajax script to update the mysql database when the player is movine over the map. 问题是当播放器在地图上移动时,我不知道下一步该怎么做或如何配置ajax脚本来更新mysql数据库。

Here is the ajax code im trying to use. 这是我尝试使用的ajax代码。

     //calling ajax to update player location when he move around
        function send(url){
var request;
try{
    request= new XMLHttpRequest();
} catch (e){
    try{
        request= new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            request= new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            alert("Your browser broke!");
            return false;
        }
    }
}
request.onreadystatechange = function(){
    if(request.readyState == 4){
        //I dont know what to add here :-(
    }
}
request.open("GET", url, true);
request.send(null);
}
send("update_location.php?newX="+ toX + "&newY=" + toY)

update_location.php update_location.php

  <?php
$new_x=$_GET['newX'];
$new_y=$_GET['newY'];
//echo"$new_x , $new_y";
$update_loc=mysql_query("UPDATE users SET location_x='$new_x' WHERE username='admin'");
?>

The main ideea is that when the player moves anywhere over the map the ajax updates new x and y values into the database.I dont need any button or jquery code added,i think ajax will work fine if somebody get me a hand doing this. 主要思想是,当玩家在地图上的任意位置移动时,ajax会将新的x和y值更新到数据库中。我不需要添加任何按钮或jquery代码,我认为如果有人帮我这样做,ajax会很好地工作。

PS toX and toY are javascript vars wich i transformed to php vars so i can update them to mysql datavase.If somebody could help me do this i would really appreciate! PS toX和toY是我转换为php vars的javascript vars,因此我可以将它们更新为mysql datavase.if有人可以帮助我做到这一点,我将不胜感激!

First sort out what you are trying to do in this case. 首先,在这种情况下,您要尝试做什么。 From your description I dont think its clear. 从您的描述中,我认为不清楚。

If you want to do something based on your ajax response, then onSucess is the right option and not onreadystatechange . 如果您想基于ajax响应来做某事,那么onSucess是正确的选择,而不是onreadystatechange

If you want to do something on Ajax response, then you should send the required data from the server side and use it to perform required actions in your javascript code. 如果您想对Ajax响应做一些事情,那么您应该从服务器端发送所需的数据,并使用它来执行JavaScript代码中的所需操作。

Eg. 例如。

new Ajax.Request('testurl',{
            method: 'post',
            parameters: {param1:"A", param2:"B", param3:"C"},
            onSuccess: function(response){
            //do something here

        }
        });

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

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