Having a problem with drawing a rectangle to the window if my variable touching is equal to true. The collision detection works fine. If I put a printf statement within the if statement at the end it prints it out. I don't understand why it's not drawing.
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Network.hpp>
#define RAND_MAX 100;
//Must get object to attach to player and follow player.
int main()
{
sf::RectangleShape player;
player.setSize(sf::Vector2f(30,30));
player.setFillColor(sf::Color::Blue);
sf::RectangleShape object;
object.setSize(sf::Vector2f(30,30));
object.setFillColor(sf::Color::Red);
sf::RectangleShape object2;
object2.setSize(sf::Vector2f(30,30));
object2.setFillColor(sf::Color::Red);
int x = rand();
int y = rand();
object2.setPosition(100,100);
sf::RenderWindow window(sf::VideoMode(1024, 600), "Game");
bool touching = false;
player.setPosition(0,0);
object.setPosition(100,100);
while (window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
player.move(-3,0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
player.move(3,0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
player.move(0,3);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
player.move(0,-3);
}
if (object.getGlobalBounds().intersects( player.getGlobalBounds())){
touching = true;
}
}
window.clear();
window.draw(player);
window.draw(object);
if (touching){
window.draw(object2);
}
window.display();
}
return 0;
}