SFML community forums

Help => Graphics => Topic started by: enderboy768 on May 14, 2017, 08:02:42 am

Title: Passing Entity Position to an If Statement
Post by: enderboy768 on May 14, 2017, 08:02:42 am
Is there any simple way to get the entity position to use it in an if statement? I want to use lit like if the moving entity reach a certain point, it will change the direction. And if you want to put any examples / explanation please put it as simple as possible because I'm just starting to learn game programming. Thank you in advance!  :)
Title: Re: Passing Entity Position to an If Statement
Post by: GameBear on May 14, 2017, 09:00:22 am
Is this entity a sfml object? If so:

NameOfEntity.getPosition().x will give you the horizontal position.

NameOfEntity.getPosition().y will give you the vertical position.

Example:
float xSpeed = 1, ySpeed = 0.2;
entity.move(xSpeed, ySpeed);
If(entity.getPosition().x > 500) {
  xSpeed = xSpeed * -1;
  entity.move(xSpeed, 0);
}

else If(entity.getPosition().x < 10) {
  xSpeed = xSpeed * -1;
  entity.move(xSpeed, 0);
}

 

(written from my phone, so sorry for any typos)
This code will have entity move back and forward horizontally
It will also move down vertically, but I have not added any code to make it change the y direction...
Hint: it's nearly the same as the one that chanqged the x direction...

Best regards
Title: Re: Passing Entity Position to an If Statement
Post by: enderboy768 on May 14, 2017, 11:16:21 am
Ah, thank you very much!  :D And yes, by entity I meant object!