-
I'm having trouble getting my sprite to move through a function. The way it works is I pass a pointer to the function with the location of the sprite and my rectangle shape, and then I check for collision in the if statement by dereferencing the variables. The problem with this is that it's giving me errors and won't let me dereference sprite1 and rect in the if condition. I know the function runs because eveytime a collision is made it displays "MOVE!", but unfortunately it doesn't move the sprite's y variable the opposite of gravity. Can anyone tell me what I'm doing wrong and how I can get this function to work properly? I would greatly appreciate it ;D
void interaction (sf::Sprite* sprite1, sf::RectangleShape* rect){
if(*sprite1.getGlobalBounds().intersects(*rect.getGlobalBounds()) == true){
*rect.setFillColor(sf::Color::Black);
*sprite1.move(0, -gravity);
cout<<"MOVE!";
}
}
-
When dereferencing pointers I always use the '->' notation instead of (*), I feel its just easier to read and understand.
So maybe try:
if(sprite1->getGlobalBounds().intersects(rect->getGlobalBounds()) == true){
rect->setFillColor(sf::Color::Black);
sprite1->move(0, -gravity);
cout<<"MOVE!";
}
OR, not sure on the syntax correctness since I use ->, but I think if you use the * approach it should be
if((*sprite1).getGlobalBounds().intersects((*rect).getGlobalBounds()) == true){
(*rect).setFillColor(sf::Color::Black);
(*sprite1).move(0, -gravity);
cout<<"MOVE!";
}
-
Turns out I just called the function with the wrong paramaters by accident :P Thanks for the response though :D I also figured out that the variables were address locations (or so I think?), so this ended up working for me :
void interaction (sf::Sprite &sprite1, sf::RectangleShape &rect){
if(sprite1.getGlobalBounds().intersects(rect.getGlobalBounds()) == true){
rect.setFillColor(sf::Color::Black);
sprite1.move(0, -gravity);
cout<<"MOVE!";
}
}
calling it like this
interaction(sprite1,rect);