Hey guys,
I'm beginner in SFML so please don't be too strict ;)
If I press the right Key for example, a sprite moves.
vehicle.move(speed,0);
Nothing special.
But now I want to create a new Sprite and according to which sprite I choose (by mouse click), I want that the correct sprite moves. To see which sprite was taken I've got a simple variable (int choose=0 if sprite 1 was taken and choose=1 if sprite 2 was taken).
The easiest way to find out which sprite should be moved is
if(choose==0)
{
...
}
if(choose==1)
{
...
}
But in that codes would stand exactly the same. That's not really effective. So can I create a sprite like
sf::Sprite vehicle[2](vehicle1,vehicle2)
...
vehicle[choose].move(speed,0)
Sorry, I know the code is wrong, I'm not really good at using arrays.
Or is there any other simple solution?
Thanks,
5gum
Keep a STL container with all the sprites:
std::vector<sf::Sprite> sprites;
sprites.push_back(...);
To choose one sprite, check for every one if the mouse position is located inside (do this upon a sf::MouseButtonPressed event):
sf::Sprite* chosenOne = nullptr;
...
for (sf::Sprite& sprite : sprites)
{
if (/* mouse inside sprite*/)
chosenOne = &sprite;
// Hint: look at sf::Event variable,
// sf::FloatRect::contains() and sf::Sprite::getGlobalBounds()
}
Then move the selected sprite:
if (chosenOne)
chosenOne->move(...);