cooldude832 Posted July 25, 2007 Share Posted July 25, 2007 This following portion of the script is for a game i'm making and it is to move the character around the map <?php //$row is a query off the user table for general information //Makes sure you defined a direction before wasting resources if(!empty($_POST['direction'])){ //Takes the variables off the posting $x = 0; $y = 0; list($x,$y) = explode("_",$_POST['direction']); //If you didn't move why waste the time querying Probably will never happen, but incase a glitch we have it covered if($x == $row['X'] && $y == $row['Y']){} else{ connectSQL(); $mapQ = "Select Type From `map` Where X = '$x' && Y = '$y'"; $mapR = mysql_query($mapQ) or die(mysql_error()); $mapRow = mysql_fetch_assoc($mapR); //Checks to see if you can walk on that square if($mapRow['Type'] != 1 && $mapRow['Type'] != 6 && $mapRow['Type'] != 7 && $mapRow['Type'] != { $MapU = "Update `Users` Set X='$x', Y='$y' Where UserID = '$UserID'"; $MapUR = mysql_query($MapU) or die(mysql_error()); $x = $x; $y = $y; $x1 = $x+1; $x2 = $x-1; $y1 = $y+1; $y2 = $y-1; $cordset = "yes"; } } } if($cordset != "yes"){ $x = $row['X']; $y = $row['Y']; $x1 = $x+1; $x2 = $x-1; $y1 = $y+1; $y2 = $y-1; } //This is map displaying function (farther down page) $Xmin = $row['X']-5; $Ymin = $row['Y']-5; $Xmax = $row['X']+5; $Ymax = $row['Y']+5; $mapQ = "SELECT * FROM map WHERE X >= '$Xmin' && Y >= '$Ymin' && X <= '$Xmax' && Y <= '$Ymax'"; $mapR = mysql_query($mapQ) or die(mysql_error()); $count = mysql_num_rows($mapR); if($count>0){ $i = 0; while($rowMap = mysql_fetch_array($mapR)){ $tempkey = "\"map_".$rowMap['Y']."_".$rowMap['X']."\""; $map[$tempkey]['x'] = $rowMap['X']; $map[$tempkey]['y'] = $rowMap['Y']; $map[$tempkey]['type'] = $rowMap['Type']; $i++; } echo "<tr><td> <table id=\"map\" cellpadding=\"0\" cellspacing=\"0\">"; $i = $Ymin; while($i<=$Ymax){ echo "<tr>"; $j = $Xmin; while($j<=$Xmax){ $tempkey = "\"map_".$i."_".$j."\""; echo "<td class=\"map_".$map[$tempkey]['type']."\">"; //Puts character image in center of map if($cordset != "yes"){ if($j == $row['X'] && $i == $row['Y']){ echo "<img src=\"images/characters/".$row['Chr'].".png\" alt=\"Character Image\" width=\"48\" height=\"40\" />"; } } else{ if($j == $x && $i == $y){ echo "<img src=\"images/characters/".$row['Chr'].".png\" alt=\"Character Image\" width=\"48\" height=\"40\" />"; } } echo "</td>\n"; $j++; } echo "</tr>"; $i++; }//End of Map Displaying Functions ?> Notes: I see some possibilities like instead of using more if statements create comparrasion values in upper level if statements, but otherwise i'm open to ideas that will speed it up. It basically centers your character on a map and allows you to move. to see it in action go to http://pira00.worldispnetwork.com/kash/play.php User: tester password: tester Please don't make any accounts i will just delete them Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 25, 2007 Share Posted July 25, 2007 i think your sql is fine but the way you name your variable it seems like your just doing an example and are u sure you dont want to use JS? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 25, 2007 Author Share Posted July 25, 2007 how so? I think it makes sense to me (i see a few bad names, but otherwise it seems good) Its a coordinate system so thus (x,y) are the parameters of a Cartesian plane not just random names i used How so use javascript? I use a bit here <MAP NAME="navi"> <AREA SHAPE="RECT" COORDS="47,1,85,45" nohref onClick="document.navi.direction.value='<?php echo $x."_".$y2;?>'; navi.submit();" TITLE="North"> <AREA SHAPE="RECT" COORDS="85,47,128,80" nohref onClick="document.navi.direction.value='<?php echo $x1."_".$y;?>'; navi.submit();" TITLE="East"> <AREA SHAPE="RECT" COORDS="54,65,82,117" nohref onClick="document.navi.direction.value='<?php echo $x."_".$y1;?>'; navi.submit();" TITLE="South"> <AREA SHAPE="RECT" COORDS="0,49,51,70" nohref onClick="document.navi.direction.value='<?php echo $x2."_".$y;?>'; navi.submit();" TITLE="West"> But as you see here the directions actually hold the cords of that square so that way you can't refresh your way around the map instead you have to manually move (more advertising money that way ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.