Creating Objects Problems with my Players

Started by
6 comments, last by Zahlman 18 years, 2 months ago
im having problems with the code while making my football game Here is the layout of my game GAME(main.cpp) --- HAS 20---> PLAYER (player.cpp(base class)) My problem is that when i create 10 Player objects for team 1, and 10 for team 2, all are using a single objects values. If i set team 1 - player 1 to be 100m tall every player in team 1 and team 2 become 100m tall if i choose team 1 players all to have red shirts and team 2 players all have blue shirts, ALL players (both teams) end up wearing red shirts. -------------------------------------------------------------------- Here is my code inside GAME AttackPlayer *Attacker[10]; for(count =0; count< 10; count++) { Attacker[count] = new AttackPlayer; Attacker[count] -> InitialisePlayer(TEAM1); } for(count =0; count< 10; count++) { AttackerData[count][rotation] = AttackerSetRunFacing(count); Attacker[count]->PlayerPrepare(AttackerData[count][RunAniSpeed]); Attacker[count]->DrawPlayer(AttackerData[count][currentx], AttackerData [count][currenty], AttackerData[count][currentz], AttackerData[count][rotation]); } ------------------------------------------------------------------------------ TEAM1 is a variable passed to the initialisation function of the player class. It tells it which team shirt the player should wear. Other values like player height, colour and animation speed are passed from GAME to the individual PLAYER. Attackerdata is a double array holding the individuals stats like location and current status in the game world. ------------------------------------------------------------------------------ I think its because i have confused pointers to objects and actual objects or something, I want each of the 20 players to have their own PLAYER object with their own variables. Please Help :) Paul
Advertisement
I don't think the bug is here. Why don't you post your headers.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
sounds like you are using a static somewhere that you shouldn't be.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
AttackPlayer Header file
--------------------------------------------------------------------
class CTargaImage;

class AttackPlayer
{
private:




public:

AttackPlayer();
virtual ~AttackPlayer();

bool Shutdown();

void InitialisePlayer(int whotheyr);

void PlayerDrawCube(float xPos, float yPos, float zPos);

void PlayerDrawFoot(float xPos, float yPos, float zPos);

void PlayerDrawArm(float xPos, float yPos, float zPos);

void PlayerDrawHead(float xPos, float yPos, float zPos);

void PlayerDrawTorso(float xPos, float yPos, float zPos);

void PlayerDrawLeg(float xPos, float yPos, float zPos);

void DrawPlayer(float xPos, float yPos, float zPos, float Rotates);

void PlayerPrepare(float dt);

};

Attack Player Cpp file
------------------------------------------------------------------------------

#define TEXclear 0
#define TEXskin 1
#define TEXfrontshirt 2
#define TEXbackshirt 3
#define TEXtopsideshirt 4
#define TEXarm 5
#define TEXshorts 6
#define TEXface 7
#define TEXhair 8

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include <time.h>
#include "AttackPlayer.h"
#include "FileHandler.h"


char legStates[2];
char armStates[2];

float legAngles[2];
float armAngles[2];

const char LEFT = 0;
const char RIGHT = 1;

const char BACKWARD_STATE = 0;
const char FORWARD_STATE = 1;
float rotationAngle = 2.0f;

int TextureSet;

unsigned int attackTex[9];
FileHandler *attackFileGet = NULL;

int chosenTex[6];


AttackPlayer::AttackPlayer()
{
armAngles

= 0.0;
armAngles

= 0.0;
legAngles

= 0.0;
legAngles

= 0.0;

armStates

= FORWARD_STATE;
armStates

= BACKWARD_STATE;

legStates

= FORWARD_STATE;
legStates

= BACKWARD_STATE;
}

AttackPlayer::~AttackPlayer()
{
}

void AttackPlayer::InitialisePlayer(int whotheyr)
{
TextureSet = whotheyr;

attackFileGet = new FileHandler;

attackFileGet -> InitialiseTextures(attackTex, TextureSet);
chosenTex[0] = TEXclear;
}

... well yes. Your AttackPlayer class doesn't have any member data; each instance is instead referring to global variables in the AttackPlayer.cpp. What do you *expect* to happen?
Ahhh right, im not the best programmer in the world and always make silly mistakes and forget these types of rules.

Could you please show me how to change my code to allow each instance of my PLAYER class to have its own member variables.

I want each player to have his own height/ animation speed etc
HAHAHA

Just realised how much of a stupid question my last post was!!

I copied my variables into the header file under Private and all works well!!!


Thanks so much for all the help now im one happy bunny!!

Paul
Yeah, sometimes all you have to do is listen to yourself :)

This topic is closed to new replies.

Advertisement