I have been bashing my head against the wall for the last 4 days trying to figure out where in the name of holy pasta salad I'm going wrong here. I cannot begin to vent my frustration.
class AnimalObj{
sf::Sprite animalSprite;
void MoveAnimal(int direction){
//Moves in the appropriate direction
if (direction == 1){
//Go up
if (animalSprite.getPosition().y - animalMoveDist >= 0){
animalSprite.move(0.0, -animalMoveDist);
AnimateAnimal(1, 1);
previousMove = 1;
}
}else if (direction == 3){
//Go down
if (animalSprite.getPosition().y + animalMoveDist <= 800){
animalSprite.move(0.0, animalMoveDist);
AnimateAnimal(3, 3);
previousMove = 3;
}
}else if (direction == 2){
//Go right
if (animalSprite.getPosition().x + animalMoveDist <= 1000){
animalSprite.move(animalMoveDist, 0.0);
AnimateAnimal(2, 2);
previousMove = 2;
}
}else if (direction == 4){
//Go left
if (animalSprite.getPosition().x - animalMoveDist >= 0){
animalSprite.move(-animalMoveDist, 0.0);
AnimateAnimal(4, 4);
previousMove = 4;
}
}else{
AnimateAnimal(0, 0);
previousMove = 0;
}
}
std::vector<Chicken> animalArr;
int main{
AnimalObj NewChicken;
NewAnimal.animalSprite.setTexture(henTex);
NewAnimal.animalSprite.setPosition(400, 400);
AnimalObj new_chicken;
NewAnimal.animalSprite.setTexture(roosterTex);
animalArr.push_back(new_chicken);
while (renderWindow.isOpen()){
for(int animal = 0; animal < animalArr.size(); animal++){
animalArr[animal].MoveAnimal(RandomDirection()); //RandomDirection returns a random direction
renderWindow.draw(animalArr[animal].animalSprite);
}
}
}
I hope I didn't post too much code. I purged as much as I could that I thought was surplus/unnecessary.
The problem is that each sprite will move in the same direction, regardless of where they are going. If one goes left, all sprites go left. If it goes right... all of them go right. At first I thought it was an issue with my random number generation, but I programmed in instructions to move to a particular position for one of the sprites, and all sprites would then move in the appropriate direction, regardless of where they are spawned. Once the first sprite arrives at location, it stops moving. I've pretty much narrowed down the error to the MoveAnimal function that I posted above, but I have no idea why it's doing that. It seems that the .move() function is causing all sprites in the vector to move in the same direction
despite them being told to move randomly I checked, the appropriate MoveAnimal are receiving instructions to move randomly, but are ignoring them in favor of following the direction of the first sprite until it arrives at location, then all the rest continue to move in sync.
I don't know if I'm just being incredibly dense here, but any help will be greatly appreciated. Seriously. I'd like to stop banging my head on the table, so if anyone can help me put an end to that, that would be great.