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

Author Topic: Sprite Pointers  (Read 1753 times)

0 Members and 1 Guest are viewing this topic.

randy808

  • Newbie
  • *
  • Posts: 6
    • View Profile
Sprite Pointers
« on: July 12, 2013, 02:07:12 am »

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!";

       
               
       

}
}

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Sprite Pointers
« Reply #1 on: July 12, 2013, 02:22:51 am »
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!";
}

randy808

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Sprite Pointers
« Reply #2 on: July 12, 2013, 02:26:36 am »
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);                            
« Last Edit: July 12, 2013, 02:34:09 am by randy808 »

 

anything