Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Passing Entity Position to an If Statement  (Read 2797 times)

0 Members and 1 Guest are viewing this topic.

enderboy768

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Passing Entity Position to an If Statement
« 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!  :)

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Passing Entity Position to an If Statement
« Reply #1 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
string message = "some random dude";
cout << "I'm just " << message;

enderboy768

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Passing Entity Position to an If Statement
« Reply #2 on: May 14, 2017, 11:16:21 am »
Ah, thank you very much!  :D And yes, by entity I meant object! 

 

anything