Page 1 of 2
other class cant change variables in another class.
Posted: Sat Jan 24, 2009 4:12 pm
by Vortex
hello i have a problem,
lets say my player1 class needs to execute a function in the player2 class, that works fine, the function will change 1 boolean and then it will change the sprite of a specific surface.
however it appears that it dosent change the boolean in the player2 class. ive tryed to write out if the boolean is changed or not and it sais it is not so how do i make it change it??
also sorry for making so many topics with my problems, game programming is harder then i tough.
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 4:18 pm
by programmerinprogress
Could you elaborate, I don't quite understand what you're asking, sorry...
EDIT: I don't want to misunderstand your question and tell you the wrong answer, could you explain what you're trying to achieve overall?
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 4:33 pm
by Vortex
okay ill explain a bit more,
you see ive got a player1 class that in one of its functions checks for collision with a player2 class alright?
if it found an collision it will run the P2.tagged() function ( P2 is a player2 class and .tagged() is a function that changes a boolean variable to true and change the sprite of the PLAYER2 surface alright? )
in the "game" when player1 collide with player2 the PLAYER2 surface changes sprite so i assume that the function was executed
however if the boolean is changed to true the player2 class shouldent be able to move and this works cause when player2 collide with player1 the variable is changed via the tagged() function in the player2 class. and player2 wont be able to move. so my problem is that player2 still can move and that it wont change the variable.
if u want ill post the classes or parts of them.
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 4:46 pm
by programmerinprogress
I think your design sounds a little flawed.
I would do it a different way.
I would do something like this:
Code: Select all
class PlayerOne
{
public:
void setTag();
private:
bool tagged;
};
void PlayerOne::setTag()
{
Tag = true; // or Tag = !Tag if you want to reverse the action each time you collide
}
class PlayerTwo
{
public:
setTag(); // this is of course if you want to set tags for the other player, you would just do the same thing as above
private:
bool tagged;
};
// some separate function (for now)
bool Check_Collision(PlayerOne P1, PlayerTwo P2)
{
// collision stuff , return a true or false
}
int main(int argc char* argv[])
{
PlayerOne P1;
PlayerTwo P2;
if(Check_Collision(P1,P2))
{
P1.setTag();
}
}
Or Something to that effect...
Basically, Accessors are used to change the tagged value to true, if there's a collison.
I mean you could write a checkCollision function, which is a method of PlayerOne I guess, but you would have to be sure to either declare + define the Player to collide with first, or simply declare that there will be a class to collide with, like this;
Code: Select all
class PlayerTwo;
class PlayerOne
{
// class crap
bool Check_Collision(PlayerOne P1, PlayerTwo P2);
}
class PlayerTwo
{
}
another alternative is to perhaps come up with a common ' player' class and derive two different types of player from that base class...
if nothing really differs between a playerTwo class and PlayerOne, then just declare two Objects, and test them against eachother.
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 4:56 pm
by Vortex
okey, now im really confused........
i did like you said and checked for collision in my main function
something like this:
Code: Select all
int main(int argc char* argv[])
{
PlayerOne P1;
PlayerTwo P2;
while(1)
{
P1.handle_input() // handles input from both players.
P2.handle_input()
if(P1.alive())
{
P1.move();
}
if(P2.alive())
{
P2.move();
}
if(Check_Collision(P1,P2))
{
P1.setTag();
}
more nonrelated stuff.
}
}
now when they collide nothing happens ( before atleast it changed the sprite. ) exept from them stopping to move wich i check in the move() function...
it might be cause im really tired or something is really wrong here...
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 5:00 pm
by programmerinprogress
did you program it so that the sprite changes based on the value of the tag member(or whatever you called it)?
clearly you tested movement based on that, but you also have to tell the thing to use another picture when they collide or something to that effect.
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 5:08 pm
by Vortex
Code: Select all
void player2::tagged()
{
isDead = true;
SDL_FreeSurface( PLAYER2 );
PLAYER2 = NULL;
PLAYER2 = load_image( "files\\pictures\\frozen.png" );
show();
}
sorry dont really understand what you mean, heres the function that changes the sprite however.
but i dont think the function is executed cause when i checked for collision inside player1 it atleast changed the sprite, and now it should atleast also change the sprite cause i didint change anything exept from checking for collision in the main function....
im really confused..
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 5:16 pm
by programmerinprogress
My example was deliberately abstract, to show you how YOU could implement a similar solution (it was more about the algorithm than the exact meaning)
however, you could solve your problem by doing this
Code: Select all
if(Check_Collision(P1,P2))
{
P2.tagged(); // this is YOUR version, this should change the sprite and such, my example was using a hypothetical scenario
}
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 5:25 pm
by Vortex
what??...
that´s exactly what im doing in the main function
Code: Select all
if( check_collision(P1,P2) )
{
P2.tagged();
}
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 5:28 pm
by programmerinprogress
in the code snippet you showed me it calling P1.setTag(); not P2.tagged();
I've obviously misguided you,I think it would require me to write out a shedload of code to explain this one, and you know it's too late, so trying figure it out, I tried my best to assist you.
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 5:48 pm
by Ginto8
If this is a game of tag, you could do something like this:
Code: Select all
if( collides( P1, P2 ) )
{
if( P1.is_it() )
P2.tagged();
else if( P2.is_it() )
P1.tagged();
}
Re: other class cant change variables in another class.
Posted: Sat Jan 24, 2009 6:01 pm
by Vortex
programmerinprogress wrote:in the code snippet you showed me it calling P1.setTag(); not P2.tagged();
I've obviously misguided you,I think it would require me to write out a shedload of code to explain this one, and you know it's too late, so trying figure it out, I tried my best to assist you.
well thanks anyways.
also i just wrote setTag() cause you wrote it in my program i use tagged(),
Re: other class cant change variables in another class.
Posted: Sun Jan 25, 2009 1:07 pm
by LeonBlade
I smell a noob... lol...
It's cool, we all were noobs at one point...
Re: other class cant change variables in another class.
Posted: Sun Jan 25, 2009 1:22 pm
by programmerinprogress
Vortex is cool, he just needs to pick a few things up (and I need to explain things better)
Re: other class cant change variables in another class.
Posted: Sun Jan 25, 2009 1:40 pm
by LeonBlade
Yeah he is...
If you want to change variables, use a Public Get/Set