I figured out most of the problem by myself now. With
ASprite * copy = new ASprite((**it2));
giveToFunction(copy);
it worked, finally, allthough I thought I tried it before.
The Problem now reduced to:
My Input Class should tell, if the mouse is Pressed (once for a single click). What it does is saying yes for every iteration of the program during clicking - like a held down function.
I've tried my function with minimal setup and it worked - so 1 click = 1 pressed event.
I think the problem is about storing the event. The Lock I implemented here didn't do its job..
How should input be handled? I'm new to this whole thing and figured that accessing the Input from everywhere would be very practical (that's why Singleton)! Like the player could use the Input by himself, without the need for some container to search for him. Any suggestion would be fine
.
My Input looks like this:
Input.cpp
#include "Input.h"
#include <iostream>
void Input::update(sf::Event event)
{
this->event = event;
eventLock = false;
//KEYBOARD
if(event.type == sf::Event::KeyPressed)
{
down.insert(event.key.code);
return;
}
else if (event.type == sf::Event::KeyReleased)
{
down.erase(event.key.code);
return;
}
//MOUSE
else if(event.type == sf::Event::MouseButtonPressed)
{
down.insert(event.mouseButton.button);
return;
}
else if (event.type == sf::Event::MouseButtonReleased)
{
down.erase(event.mouseButton.button);
return;
}
else if (event.type == sf::Event::MouseMoved)
{
mousePosition.x = event.mouseMove.x;
mousePosition.y = event.mouseMove.y;
//std::cout << mousePosition.x << " " << mousePosition.y << std::endl;
return;
}
}
bool Input::pressed(int value)
{
if(eventLock) return false;
if( (event.type == sf::Event::KeyPressed || event.type == sf::Event::MouseButtonPressed)
&& event.key.code == value)
{
eventLock = true;
return true;
}
return false;
}
Input.h
#ifndef __GutenMapEdit__Input__
#define __GutenMapEdit__Input__ 1
#include <iostream>
#include <set>
#include <SFML/Graphics.hpp>
class Input
{
public:
void update(sf::Event event);
bool pressed(int value);
bool heldDown(int value);
sf::Vector2i getMousePosition(){return mousePosition;}
private:
std::set<int> down;
sf::Vector2i mousePosition;
sf::Event event;
bool eventLock = false;
/* Singleton */
public:
static Input& instance()
{
static Input _instance;
return _instance;
}
private:
Input () { };
/* prohibition for copying a new instance */
Input ( const Input& );
~Input () { };
};
Where the main calls:
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{ window.close(); }
Input::instance().update(event);
}// Process events ^
I am completely self taught and any help how to do it better would be great. But please, spare words like ugly. No beginner is perfect, neither are you. Thanks in advance!