Author Topic: Movement  (Read 1322 times)

Offline timpog

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Movement
« on: August 15, 2010, 05:35:56 PM »
Hey, Im currently developing a new game, and I'm stuck on the most basic thing: Movement.  Im pretty sure something is wrong with the update query so could you help me out(Im gonna post the whole code in case the problem is somewhere else.)

index.php:
Code: [Select]
<?php

include "library.php";
include 
"func.php";
connect();

$userquery mysql_query("SELECT * FROM users WHERE id='1' LIMIT 1");
$userrow mysql_fetch_array($userquery);

$do explode(":"$_GET['do']);

//Now check what to do
if($do[0] == "move") { move($do[1]); }

echo 
$userrow["position_x"];
echo 
"<br/>" $userrow["position_y"];
?>

func.php:
Code: [Select]
<?php

function move($dir) {

$x $userrow["position_x"];
$y $userrow["position_y"];
if($dir == "n") { $x2 $x 1$string "position_x='$x2'"; }
if($dir == "s") { $x2 $x 1$string "position_x='$x2'"; }
if($dir == "e") { $y2 $y 1$string "position_y='$y2'"; }
if($dir == "w") { $y2 $y 1$string "position_y='$y2'"; }
if($dir == "") { header("Location: index.php"); }
//Now update the database.
mysql_query("UPDATE users SET $string WHERE id='1' LIMIT 1");

}

?>

When you see "WHERE id='1'" That is only for debugging.  Thanks guys!

Offline timpog

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Movement
« Reply #1 on: August 15, 2010, 07:05:42 PM »
please post if you know the problem

Offline Nox

  • Level 35
  • **
  • Posts: 738
  • Reputation: +12/-2
    • View Profile
Re: Movement
« Reply #2 on: August 15, 2010, 07:17:22 PM »
1) Calm down, it's been just 2hrs (and at least in my country it's actually 02:00 in night)...
2) You need to learn to solve these smaller issues for yourself...var_dumping* variables and echoing strings and queries is usually a good bet, commenting out code if you search for some PHP error etc.
3) You can use "position_x = position_x - 1" etc. though it would not necessarily be the case but coupled with - have you checked the $x, $y variables*? You don't pass $userrow into the function so they are probably undefined and therefore it's not working

*) http://us.php.net/manual/en/function.var-dump.php
Meet us at an IRC irc.freenode.net #bbg as well
Enjoy http://spiritbeacon.noxart.cz/ !

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,134
  • Reputation: +26/-1
    • View Profile
Re: Movement
« Reply #3 on: August 16, 2010, 03:12:50 AM »
Add exit() / return after header(). header("Location: xxx"); is HTTP command, it DOES NOT stop PHP execution.

Add echo mysql_error(); inside move() function, this might help you debugging this.

LIMIT in update looks suspicious to me, can we even use LIMIT with UPDATE?

If you create 'id' as an AUTO_INCREMENT PRIMARY KEY you don't need to type LIMIT anywhere (since it is unique anyway).

Read about "global" and variable visibility/scope.

2) You need to learn to solve these smaller issues for yourself...
I like that statement :D

Offline doublet

  • Level 3
  • *
  • Posts: 8
  • Reputation: +0/-0
    • View Profile
Re: Movement
« Reply #4 on: August 16, 2010, 04:56:51 AM »
As said before: set $userrow as global variable in index.php
Also from math point x-axis should be "west-east" and y-axis "north-south".

Offline timpog

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Movement
« Reply #5 on: August 16, 2010, 10:57:25 AM »
Hmm, thanks I never really knew what global was but now I do. 
And thanks doublet...  :P

Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: Movement
« Reply #6 on: August 17, 2010, 06:02:00 AM »
As said before: set $userrow as global variable in index.php

Although that will work, using unnecessary globals is generally considered bad practice (in any language, not specific to PHP) and getting into the habit of using them is a good way of creating weird, hard-to-debug problems for yourself when you start doing more complex things with your code.

A better solution would be to revise the "move" function to take a location along with the direction so that it doesn't need to know $userrow exists or have direct access to it.

Offline timpog

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Movement
« Reply #7 on: August 17, 2010, 11:13:24 AM »
Ok, but directions and locations together is a bit more technical for right now, im going to be just building the basics and then I'll start doing revisions and adding more like directions.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal