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'");
?>