1
General / How to make setters
« on: September 26, 2021, 09:59:20 am »
How to make setters on sfml parameters?
I transfer functions from the Player class to the Game class, so that most interactions are in this class, I transfer the collision function, for example, a line in the Player class:
where Pbody is FloatRect;
so I'm trying to do in the Game class
writes expression must have mudifiable lvalue error
so here setter and getter and the function itself
I transfer functions from the Player class to the Game class, so that most interactions are in this class, I transfer the collision function, for example, a line in the Player class:
this-> pBody.left = j * BoxSize - this-> pBody.width;
where Pbody is FloatRect;
so I'm trying to do in the Game class
this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize - this-> player-> getPBody (). width;
writes expression must have mudifiable lvalue error
so here setter and getter and the function itself
void Player :: setPBodyLeft (float x)
{
this-> pBody.left = x;
}
const sf :: FloatRect Player :: getPBody () const
{
return this-> pBody;
}
void Game :: PlayerMapCollision (float dir)
{
for (int i = this-> player-> getPBody (). top / BoxSize; i <(this-> player-> getPBody (). top + this-> player-> getPBody (). height) / BoxSize; i ++ )
for (int j = this-> player-> getPBody (). left / BoxSize; j <(this-> player-> getPBody (). left + this-> player-> getPBody (). width) / BoxSize; j ++ )
{
if (TileMap [i] [j] == 'A')
{
if (dx> 0 && dir == 0)
this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize -this-> player-> getPBody (). width;
this-> player-> getPBody (). left = j * BoxSize - this-> player-> getPBody (). width;
if (dx <0 && dir == 0)
this-> player-> getPBody (). left = j * BoxSize + BoxSize;
if (dy> 0 && dir == 1)
{
this-> player-> getPBody (). top = i * BoxSize - this-> player-> getPBody (). height;
dy = 0;
OnGround = true;
}
if (dy <0 && dir == 1)
{
this-> player-> getPBody (). top = i * BoxSize + BoxSize;
dy = 0;
}
}
if (TileMap [i] [j] == 'o')
{
TileMap [i] [j] = '';
this-> health + = 10;
this-> helthbar.setSize (sf :: Vector2f (health + 10, 20));
std: cout << health;
}
}
}
{
this-> pBody.left = x;
}
const sf :: FloatRect Player :: getPBody () const
{
return this-> pBody;
}
void Game :: PlayerMapCollision (float dir)
{
for (int i = this-> player-> getPBody (). top / BoxSize; i <(this-> player-> getPBody (). top + this-> player-> getPBody (). height) / BoxSize; i ++ )
for (int j = this-> player-> getPBody (). left / BoxSize; j <(this-> player-> getPBody (). left + this-> player-> getPBody (). width) / BoxSize; j ++ )
{
if (TileMap [i] [j] == 'A')
{
if (dx> 0 && dir == 0)
this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize -this-> player-> getPBody (). width;
this-> player-> getPBody (). left = j * BoxSize - this-> player-> getPBody (). width;
if (dx <0 && dir == 0)
this-> player-> getPBody (). left = j * BoxSize + BoxSize;
if (dy> 0 && dir == 1)
{
this-> player-> getPBody (). top = i * BoxSize - this-> player-> getPBody (). height;
dy = 0;
OnGround = true;
}
if (dy <0 && dir == 1)
{
this-> player-> getPBody (). top = i * BoxSize + BoxSize;
dy = 0;
}
}
if (TileMap [i] [j] == 'o')
{
TileMap [i] [j] = '';
this-> health + = 10;
this-> helthbar.setSize (sf :: Vector2f (health + 10, 20));
std: cout << health;
}
}
}