Jump to content

Problem with php class


Kitu

Recommended Posts

Hi,

I am writing my first script using OOP, however there is a problem I can't solve no matter how many tutorials/manuals I read, as it just looks fine to me and returns no error message(s).

var $tile;
function setTile( $symbol ) 
{
   $this->tile=$symbol;
}
  
function getTile() 
{
   return $this->tile;
}

It's just regular geter/seter stuff, but it just refuses to work...It is probably just some small, stupid error, but I still can't find it :\

Link to comment
Share on other sites

Works fine for  me:

<?php

class tileCls {

    var $tile;

    function setTile($symbol) {
        $this->tile = $symbol;
    }

    function getTile() {
        return $this->tile;
    }
}

$tile = new tileCls;
$tile->setTile('my tile');
echo $tile->getTile(); // returns: 'my tile'

?>

How are you initiating the class?

Link to comment
Share on other sites

OK. How are calling these function in the class? In order to call a method (function) that is in the same class you do this:

$this->myFunctionName();

 

So to call the setTile function you do this:

$this->setTile('my tile');

 

$this-> is is used to access methods and objects (variables) that in the same class.

Link to comment
Share on other sites

Thanx for answering so fast :)

The problem is, that I am currently using them like that...I'll copy the exact lines just in case though.

In one function:

$this->setTile($map_layer[$a][$b]);

And in other:

$monster = new fight ( $this->getTile() );

Link to comment
Share on other sites

I managed to solve the last situation simply by eliminating the variable and adding a field to MySql...but I still have the same problem with other class....set_opponent_hp() and set_player_hp() do absolutly nothing.

 

<?php
class fight {

  var $player_hp, $opponent_hp, $opponent_stats;
  
  /* Contructor taksed 3 arguments: opponent - 'player' or 'monster'...determines weather the opponent 
    is player (pvp) or monster. Meaning, it tells the script whitch table it should query for opponents 
    stats."  Opponent id is the id of opponent...if opponent is monster, it holds monster id, if opponent
    is other player it holds his/her id. Finally, attacker field determines who is the one attackig (and 
    therefore gets the first hit. Holds 2 values, eather 'player' or 'opponent'.*/
  
  function fight($opponent, $opponent_id, $attacker) 
  {
    $this->set_opponent_hp( $opponent, $opponent_id );
    $this->set_player_hp();
    while ( $this->get_player_hp() > 0 && $this->get_opponent_hp > 0 ) //While boath participants have hitpoints left 
    {
      echo 'Mõlemad on elus!<br />';
      if ( $attacker == 'player' ) //If player is the one attacking
      {
        $this->set_opponent_stats($opponent, $opponent_id);
        $this->attack($this->calculate_hit($opponent, $opponent_id), 'player'); //Let the player hit the opponent
        if ( $this->get_opponent_hp() ) //if the opponent still is alive
        {
          $this->set_opponent_stats('player', $_SESSION['User']);
          $this->attack($this->calculate_hit($opponent, $opponent_id), 'opponent'); //let the opponent hit
        }
        else //if the opponent died
        {
          break; //exit the loop
        }
      }
      else
      {
        $this->set_opponent_stats('player', $_SESSION['User']);
        $this->attack($this->calculate_hit($opponent)); //Let the opponent hit the player
        if ( $this-> get_player_hp() ) //if the player still is alive
        {
          $this->set_opponent_stats($opponent, $opponent_id);
          $this->attack($this->calculate_hit('player')); //let the player hit
        }
        else //if the player died
        {
          break; //exit the loop
        }
      }
    } 
  }
  
  function set_opponent_stats($opponent, $opponent_id) 
  {
    if ($opponent == 'monster') //if the one hiting is monster
      $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM monsters WHERE id='".$opponent_id."'") );//get the monster stats
    else //If the attacker is other player
      $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$opponent_id."'") ); //get the player stats
    $this->opponent_stats = $stats['hitpoints'];
  }
  
  function set_player_hp()
  {
    $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$_SESSION['User']."'") ); //get the player stats     
    $this->player_hp = $stats['hitpoints'];
  }
  
  function get_player_hp()
  {
    return $this->player_hp;
  }
  
  function get_opponent_hp()
  {
    return $this->opponent_hp;
  }

  function set_opponent_hp( $opponent, $opponent_id ) //Gets the stats of "Opposing force", no matter weather the one hiting is player or monster
  {
    if ($opponent == 'monster') //if the one hiting is monster
      $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM monsters WHERE id='".$opponent_id."'"));//get the monster stats
    else //If the attacker is other player
      $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$opponent_id."'")); //get the player stats     
    $this->opponent_stats = $stats;
  }
  
  function get_opponent_stats() //Returns the opponent stats
  {
    return $this->opponent_stats;
  }
  
  function calculate_hit( $attacker, $attacker_id )
  {
    if ($attacker == 'monster') //if the one hiting is monster
      $stats = mysql_fetch_assoc(mysql_query( "SELECT * FROM monsters WHERE id='".$attacker_id."'"));//get the monster stats
    else //If the attacker is other player
      $stats = mysql_fetch_assoc(mysql_query( "SELECT * FROM user_stats WHERE id='".$attacker_id."'")); //get the player stats 
    if ( $this->check_if_hit($stats['attack'],$stats['luck']) ) //checks if the attacker hit or missed 
    {
      srand(time());
      $hit = (rand()%$stats['attack']+$stats['agility']);
      return $hit; 
    }
    else //if attacker missed
    {
      return 0; //return 0 as the hit value
    }   
        
  }
  
  function check_if_hit( $accuracy, $luck ) //Calculates weather the attacker hits or not
  {
    $opponent_stats = get_opponent_stats(); //gets opponent stats
    $total = $opponent_stats['defence']+$accuracy; //calculates the total of opponent defence+user attack
    srand(time());
    $random = (rand()%$total);
    if ( $opponent_stats['defence'] > $accuracy ) //if opponents defence is better than players defence
    {
      if ( $random < $accuracy + $luck ) //if random number is smaller than attackers attack + luck
        return true; //hit
      else //if not
        return false; //miss
    }
    else //if players attack is better than opponents defence
    {
      if ( $random > $accuracy - $luck ) //if random number is smaller than attackers attack - luck
        return true; //hit
      else //if not
        return false; //miss
    }
  }
  
  function attack( $hits, $attacker ) //substracts the hit amoount of hitpoints from player/monster hp value
  {
    if ( $attacker == 'player' )
    {
      $this->player_hp = $this->player_hp-$hits;
      echo 'You hit: '.$hits;
    }
    else
    {
      $this->opponent_hp = $this->opponent_hp-$hits;
      echo 'Opponent hit: '.$hits;
    }
  }
   
} 
?>

Link to comment
Share on other sites

Can we see the code where you use this class ?

I havn't actually implemented it to my game yet, I just tested it in a stand-alone file, so it just looks like:

<?php
session_start();
include 'db.php';
include 'classes/fight.php';
$test = new fight ('monster', 1, 'player');
?>

Link to comment
Share on other sites

Lines like this....

 

$stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$_SESSION['User']."'") );

 

really ought to be avoided. It makes it very difficult to debug. Also, NEVER try and use a result resource without checking your query actually worked.

 

That one line should look more like.

 

if ($result = mysql_query ( "SELECT * FROM user_stats WHERE id='".$_SESSION['User']."'")) {
  if (mysql_num_rows($result)) {
    $stats = mysql_fetch_assoc($result);
  }
}

Link to comment
Share on other sites

I added error check to all of the querys, but the result still is the same...blank screen.

 

The methods you say 'do absolutly nothing' don't actually output anything. What code are you using to call these methods, and what output do you expect?

Link to comment
Share on other sites

I'm not actually expecting any output from these methods (except when error occures, then I expect it to output the error message), they are just supposed to set values to the variables...and, in some reason, they fail to do that and don't output any errors.

Link to comment
Share on other sites

Really, the way you have this class setup makes it pretty difficult to debug. Why are you calling all the methods within the construct instead of outside in client code?

 

Your going to need to place some sort of debug messages in the places where this thing might fail. Without those we can't really help. Your supplying absolutely no usefull infomation.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.