Jump to content
  • Advertisement
Sign in to follow this  
id0001

[web] [php/javascript] is this possible?

This topic is 3890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make some sort of browserbased text game and an important feature is the game map. At the moment I have a hardcoded map in a table which picks the links and the pictures from a database and it works great. But when there are more users using the map at the same time the map should be updated every time when a user changes the tiles on the map (you can build something on the map and it should update the pictures) and that doesn't look very well. Is there a way to only update a single tile and not having to reload the whole map? thanks in advance. ID

Share this post


Link to post
Share on other sites
Advertisement
The best forum for this question is the Web Development one, so I'll move this there.

By "hardcoded map in a table", do you mean an HTML table?

You can't really have the server notify all the clients that the map has changed. However, each client can use javascript to repeatedly ask the server (eg. every 10 seconds) if the map has changed since they last got a copy of the map, and if so, to resend it (or maybe only the changed parts). The javascript can then change the relevant table cell on that browser to the required new contents.

Typically this sort of functionality is called 'AJAX', Asynchronous javascript and XML, though often it's more useful to ignore the XML part and just have the server send plain text or JSON (a format that is easy for javascript to parse). Basically you write another PHP page that just returns these map changes, a javascript script to periodically query that page, and another script to handle the responses from that page and alter your document accordingly.

Share this post


Link to post
Share on other sites
Ok I have AJAX working now.

it updates the picture of a tile.
the only problem now is that the picture moves a little bit up when my AJAX script is executed.

Any ideas how this could be fixed?

ID

Share this post


Link to post
Share on other sites
How are you placing your pictures? Are they embedded in a table, with Div's...?
We will need to see your code and an example of what is happening before we can provide solid feedback.

Share this post


Link to post
Share on other sites
well, as you can see in the sourcecode the pictures are embedded in a table.
my system actually works fine unless one thing. when my ajax script is executed some pictures are moved a litle bit.

this is my ajax script:

function GetHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

var http = GetHttpObject();
var glob_x;
var glob_y;

function updateTable()
{
try
{
//http = GetHttpObject();

var y = 1;
var x = 1;



while(y <= 1)
{
glob_y = y;
while(x <= 3)
{
glob_x = x;
var url = "updateajaxmap.php?x="+x+"&y="+y;
http.open("GET",url,true);
http.onreadystatechange = updateTableResponse;
http.send(null);
x += 1;
}
y += 1;
}
}
catch (e)
{
alert("error1> " + e.message);
}

}

function updateTableResponse()
{
if (http.readyState == 4)
{
try
{
document.getElementById(glob_x+","+glob_y).innerHTML = http.responseText;
}
catch (e)
{
alert("error2> " + e.message);
}
}
}

function OpenPopup($link)
{
window.open($link,'window','width=400 height=500');
}



and the source of updateajaxmap.php:

<?php
include("functions.php");
sql_connect();
sql_selectDb("foo_bar");
$x = (int)htmlspecialchars($_GET['x']);
$y = (int)htmlspecialchars($_GET['y']);
if ($x <= 0 || $y <=0)
{
header("location:error.php?noret=1&errcode=Unauthorized+request");
exit();
}
$pic = mysql_query("SELECT foo FROM foomap WHERE x = $x && y = $y") or die("unable to proccess query - " . mysql_error());
$pic = mysql_fetch_row($pic);
echo ("<a href=\"javascript:OpenPopup('map.php?x=" . $x . "&y=" . $y . "')\"><Img src=\"$pic[0]\" border=\"0\"></a>");
sql_close();
?>




ID

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!