What I would like to do, is to remove units individually by right-clicking on the units concerned.(SFML 2.1+Visual Studio C++ 2010 on Win 7)
I can't get it working (creating units is no problem), because of those two if lines (marked with a comment), placed near the bottom of the code.
If I go over it, it says "Error, Expression has to be of class type". Taking away the round braces in the line, produces another type of error....Any hints on how to do it right? Many thanks!
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <list>
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(1000, 600), "Map");
mMainWindow.setFramerateLimit(30);
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(20, 20));
rectangle.setFillColor(sf::Color(255,255,255,255));
rectangle.setPosition(50, 50);
sf::Image mapimage;
mapimage.loadFromFile("map.png");
sf::Texture maptexture;
maptexture.loadFromImage(mapimage);
sf::Sprite mapsprite(maptexture);
mapsprite.setPosition(0, 0);
sf::Image unitimage;
unitimage.loadFromFile("unit.png");
sf::Texture unittexture;
unittexture.loadFromImage(unitimage);
sf::Sprite unitsprite(unittexture);
sf::RectangleShape gamelayer;
gamelayer.setSize(sf::Vector2f(1000, 600));
gamelayer.setFillColor(sf::Color(255,255,0,100));
gamelayer.setPosition(0, 0);
std::list<sf::Sprite>EnemyList;
std::list<sf::Sprite>::iterator EnemyIt;
while (mMainWindow.isOpen())
{
sf::Event event;
while (mMainWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
mMainWindow.close();
}
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
{
sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
if(gamelayer.getGlobalBounds().contains(mousecoords))
{
sf::Vector2i pixelPos = sf::Mouse::getPosition(mMainWindow);
std::cout << "x" << sf::Mouse::getPosition(mMainWindow).x << std::endl;
std::cout << "y" << sf::Mouse::getPosition(mMainWindow).y << std::endl;
sf::Sprite *Sprite;
Sprite = new sf::Sprite;
Sprite->setTexture(unittexture);
Sprite->setPosition(sf::Mouse::getPosition(mMainWindow).x,sf::Mouse::getPosition(mMainWindow).y);
EnemyList.push_back(*Sprite);
std::cout << "Objects in the list: " << EnemyList.size() << std::endl;
}
}
if(event.mouseButton.button == sf::Mouse::Right)
{
sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
if(EnemyIt->getPosition().x.contains(mousecoords)) // these both lines don't work
if(EnemyIt->getPosition().y.conatins(mousecoords))
{
EnemyList.erase(EnemyIt);
EnemyIt = EnemyList.begin();
}
}
}
mMainWindow.draw(gamelayer);
mMainWindow.draw(mapsprite);
for(EnemyIt = EnemyList.begin();EnemyIt != EnemyList.end();EnemyIt++)
{
mMainWindow.draw(*EnemyIt);
}
mMainWindow.draw(unitsprite);
mMainWindow.draw(rectangle);
mMainWindow.display();
mMainWindow.clear();
}
return 0;
}