1

I have a following problem. I'm working on a small browser based game, and I added movement with keyboard keys. It is working, but now I want to add a code, which will send the current position of player to the MySQL database. The problem is, when I push the button, for example W, my character keeps moving upward, and every step he sends a data to MySQL, creating a looooong list of PHP requests. How can I speed up this process? Here is the part of code I use:

if (key == key_W) { // Player Up
if (parseFloat(wzr.style.top)-6 < 0)
{
 $('#wzro').stop().animate({
 top: 342
 }, 0, function() {
 $('#wzro').empty();
 });
YWalk();
}

else
{
 $('#wzro').stop().animate({
 top: '-=6'
 }, 0, function() {
 $('#wzro').empty();
 });
YWalk();
}
}

function YWalk(){
            var wzr = document.getElementById("wzro");
            var xmlHttp;
            if (window.XMLHttpRequest){
            xmlHttp=new XMLHttpRequest();
            }
            else{
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlHttp.open("GET","datachodzeniey.php?y="+ wzr.style.top);
            xmlHttp.send(null);
}

And in datachodzeniey.php:

<?php
session_start();
$username = $_SESSION['username'];
$y=$_GET['y'];

$connect = mysql_connect("localhost", "root", "");
$select_db = mysql_select_db("testdb", $connect);
$q=mysql_query("update players set y='$y' where dispname='$username'");
?>
4

2 回答 2

2

首先,这是您的代码的整洁版本,没有功能更改:

//in some outer scope
var $wzro = $("#wzro");

//in the key event handler
if (key == key_W) { //Player Up
    var top = parseInt($wzro.css('top')) - 6;
    top = (top < 0) ? 342 : top;
    $wzro.empty().css('top', top);
    $.ajax({ url: "datachodzeniey.php?y=" + top });
}

现在,要减少 ajax 调用的数量:

//in some outer scope
var $wzro = $("#wzro");
var u = {//u for upload
    url: "datachodzeniey.php?y=",
    allow: true,
    reallow: function(){ u.allow = true; },
    delay: 100//(milliseconds) adjust as necessary
};

//in the key event handler
if (key == key_W) { //Player Up
    var top = parseInt($wzro.css('top')) - 6;
    top = (top < 0) ? 342 : top;
    $wzro.empty().css('top', top);
    if(u.allow) {
        $.ajax({ url: u.url + top });
        u.allow = false;
        setTimeout(u.reallow, u.delay);
    }
}

@JaspalSingh 的 memcache 想法听起来不错,可以独立于上面的代码实现。

于 2013-01-26T17:28:22.790 回答
1

这是两个请求之间间隔 20 秒的实现:

            var nowTime = new Date(); 
            var lastExecuted= new Date();

            if (key == key_W) { // Player Up
                nowTime = new Date();
                if (parseFloat(wzr.style.top)-6 < 0)
                {
                    $('#wzro').stop().animate({
                        top: 342
                    }, 0, function() {
                        $('#wzro').empty();
                    });

                } else {
                    $('#wzro').stop().animate({
                        top: '-=6'
                    }, 0, function() {
                        $('#wzro').empty();
                    });
                }
                //time interval in  milliseconds - here i have set it to 20seconds
                if (nowTime - lastExecuted >= 20000) {
                    YWalk();
                }
            }

            function YWalk(){
                lastExecuted = new Date();
                var wzr = document.getElementById("wzro");
                var xmlHttp;
                if (window.XMLHttpRequest){
                    xmlHttp=new XMLHttpRequest();
                }
                else{
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlHttp.open("GET","datachodzeniey.php?y="+ wzr.style.top);
                xmlHttp.send(null);
            }
于 2013-01-26T15:10:49.133 回答