Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [solved]Collision - want remove Sprite from List + Update  (Read 2618 times)

0 Members and 1 Guest are viewing this topic.

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
[solved]Collision - want remove Sprite from List + Update
« on: December 27, 2013, 06:19:48 pm »
Hello ^_^

im working on my project and I have a problem with the collision-class.

All is working fine and if getGlobalBounds turn to "true", the collision is detect. But.. it dont delete the Sprite from the list. It still "moving" and my list is still "full".

Please, dont be angry. Im still in practice. ._. Im trying my best. But the Collision is very hard (not to create - its only because of make OOP and use the right syntax and right return-methods(?)). So I hope you can show me, what I can do. I know, the Code is not "clean". But for the first one, it should be a good step.

Collision.hpp:
#ifndef COLLISION_HPP
#define COLLISION_HPP

#include "Enemy.hpp"
#include "Player.hpp"
#include <SFML\Graphics.hpp>
#include <list>

using namespace sf;

class Collision
{
public:
        void            Collision2Attack(std::list<sf::Sprite>enemyList,
                                                                std::list<sf::Sprite>::iterator enemyIt,
                                                                sf::Sprite hitBoxR,
                                                                RenderWindow &window
                                                                );
        void            update4List(std::list<sf::Sprite>enemyList,
                                                                std::list<sf::Sprite>::iterator enemyIt
                                                                );
};

#endif

Collision.cpp:
#include "Enemy.hpp"
#include "Player.hpp"
#include "Collision.hpp"
#include <SFML\Graphics.hpp>
#include <list>

using namespace sf;

Enemy enemy;
Player player;

void Collision::Collision2Attack(std::list<sf::Sprite>enemyList,
                                                                std::list<sf::Sprite>::iterator enemyIt,
                                                                sf::Sprite hitBoxR,
                                                                RenderWindow &window
                                                                )
{

        for(enemyIt=enemyList.begin();enemyIt!=enemyList.end();)
        {
                if( hitBoxR.getGlobalBounds().intersects(enemyIt->getGlobalBounds() ))
                {
                        //enemyIt=enemyList.erase(enemyIt);
                        enemyIt->setPosition(0,0);
                        std::cout << "delete" << std::endl;
                        Collision::update4List(enemyList, enemyIt);
                }
                else
                {
                        enemyIt++;
                }
        }

       
}

void Collision::update4List(std::list<sf::Sprite>enemyList,
         std::list<sf::Sprite>::iterator enemyIt
         )
{
        ::enemy.update4ListE(enemyList,
                                                enemyIt
                                                );
}

Enemy.cpp:
#include "Enemy.hpp"
#include "Collision.hpp"
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>

using namespace sf;

Enemy::Enemy()
{
        Speed = 300.f;
        enemyTex.loadFromFile("enemy_l.png");
        enemyTime = 0.0000;
}

void Enemy::Draw(RenderWindow &window, float ElapsedTime)
{
        for(enemyIt=enemyList.begin();enemyIt!=enemyList.end();enemyIt++)
        {
                enemyIt->move(-Speed*ElapsedTime,0);
                window.draw(*enemyIt);
        }
}

void Enemy::Process(RenderWindow &window, float ElapsedTime, float enemyTimer)
{
        enemyTime += enemyTimer;

        if(enemyTime>=9000)
        {
                enemySprite = new sf::Sprite;
                enemySprite->setTexture(enemyTex);
                enemySprite->setPosition(720, 410);
                enemySprite->setScale(0.6,0.6);
                enemyList.push_back(*enemySprite);
                enemyTime=0;
        }

        for(enemyIt=enemyList.begin();enemyIt!=enemyList.end();)
        {
                if(enemyIt->getPosition().x<=0)
                {
                        enemyList.erase(enemyIt);
                        enemyIt=enemyList.begin();
                }
                else
                {
                        enemyIt++;
                }
        }

}

void Enemy::update4ListE(std::list<sf::Sprite>enemyList,
                                                std::list<sf::Sprite>::iterator enemyIt
                                                )
{
        enemyList = enemyList;
        enemyIt = enemyIt;
       
        std::cout << static_cast<int>(enemyList.size() ) << std::endl;

}

So I didnt know, how the Enemy-Class can get the "new List", so I thought i just create a simply "update4ListE" method for updating the current List on the other class, but it isnt working.

So I want to ask you (because you all have more skills). ._.

Greetings
« Last Edit: December 27, 2013, 09:31:57 pm by kursukia »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Collision - want remove Sprite from List + Update
« Reply #1 on: December 27, 2013, 07:43:12 pm »
Scanning the code I'd guess it's because you're passing your lists by value rather than by reference - so the lists get copied into the functions, modified and then destroyed when the function exits, leaving the original list untouched.

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Collision - want remove Sprite from List + Update
« Reply #2 on: December 27, 2013, 08:59:19 pm »
Ouh, thank you fallahn :)

That was the problem. A simply problem.

I changed it into this code:
void Collision::Collision2Attack(std::list<sf::Sprite>&enemyList,
                                                                std::list<sf::Sprite>::iterator &enemyIt,
                                                                sf::Sprite hitBoxR,
                                                                RenderWindow &window
                                                                )

.. and its working now. Thank you! :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [solved]Collision - want remove Sprite from List + Update
« Reply #3 on: December 28, 2013, 05:18:11 pm »
There is no need to pass the iterator as a parameter, you can as well declare it inside the function. Keep declarations always as local as possible, i.e. inside the for loop header.

Furthermore, your method update4ListE() contains meaningless self-assignments. In general, you're doing things overly complicated. By using std::remove_if() or list::remove_if(), you can remove elements much easier and more efficiently.

Last, your code has memory leaks. There is absolutely no reason to use the new operator there.
« Last Edit: December 28, 2013, 05:21:31 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: