if(Game::GetInput().IsKeyDown(sf::Key::Space))
{
PrimaryWeapon PW1;
sf::Vector2f pos = PlayerTank::GetPosition();
if(!Game::GetGameObjectManager().Get("SmallBullet"))
{
PW1.Fire(pos);
}
}
void PrimaryWeapon::Fire(sf::Vector2f pos)
{
SmallBullet* i = new SmallBullet;
i->SetPosition(pos.x, pos.y);
Game::GetGameObjectManager().Add( "SmallBullet", i);
}
void SmallBullet::Update(float elapsedTime)
{
GetSprite().Move(Velocity * elapsedTime, 0);
sf::Vector2f pos = GetPosition();
if (pos.x >= Game::SCREEN_WIDTH)
{
Game::GetGameObjectManager().Remove( "SmallBullet");
}
}
void GameObjectManager::Remove(std::string name)
{
std::map<std::string, VisibleGameObject*>::iterator results = _gameObjects.find(name);
if (results != _gameObjects.end())
{
delete results->second;
_gameObjects.erase(results);
}
}
// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>
_STD_BEGIN
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
{
::_CrtDbgBreak();
}
}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
{ // report error and die
_Debug_message((wchar_t *) message, (wchar_t *) file, line);
}
#endif
_STD_END
for(std::list<T>::iterator it= list.begin(); it!=list.end(); ++it)
{
do something
if( condition)
list.erase(it);
}
for(std::list<T>::iterator it= list.begin(); it!=list.end();)
{
if(condition)
list.erase(it);
else{
do something
++it;
}
}
For working with it you have to do something like this:You have to assign the return value of erase(), otherwise it becomes invalid.Code: [Select][...]
list.erase(it);
[...]
map.erase(itr++)
Quote from: "TheEnigmist"For working with it you have to do something like this:You have to assign the return value of erase(), otherwise it becomes invalid.Code: [Select][...]
list.erase(it);
[...]
[...]
it=list.erase(it);
[...]
void GameObjectManager::UpdateAll(float elapsedTime)
{
std::map<int, GameObject*>::const_iterator itr = gameObjects.begin();
while (itr != gameObjects.end())
{
itr->second->Update(elapsedTime);
itr++;
}
}
void Block::Update(float elapedTime)
{
sf::Time time = clock.getElapsedTime();
if (time.asSeconds() >= 5.0f)
{
Game::GetGameObjectManager().Remove(id);
}
}
void GameObjectManager::Remove(int id)
{
std::map<int, GameObject*>::iterator result = gameObjects.find(id);
if (result != gameObjects.end())
{
delete result->second;
gameObjects.erase(result);
}
}
I know this is not my topic, and that it's probably in the wrong board, but I have exactly the same problem.You should nevertheless open a new thread, you can still reference the old one in your post.