I am trying to iterate over a vector of objects and perform a simple update function which moves the object 1 pixel to the right each time. However, when I run the code, the x value is only changed once.
In source.cpp:
for (int i = 0; i < Screen::SCREEN_WIDTH * Screen::SCREEN_HEIGHT; i++) {
people.push_back(Person());
}
for (int i = 0; i < 1; i++) {
people[i].isAlive = true;
}
while(true) {
for (Person p : people) {
if (p.isAlive == true) {
p.Update(p);
cout << p.x << endl;
screen.setPixel(p.x, p.y, 255, 0, 0);
}
else {
screen.setPixel(p.x, p.y, 0, 0, 0);
}
}
}
In person.cpp
void Person::Update(Person &person) {
person.x += 1;
}
As you can see, i select one person from the array and set them to be alive, and therefor to be drawn. They are drawn every frame, but not updated. Can anyone help me out with this?