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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - PopaF3lla010

Pages: [1]
1
Graphics / Re: Deleting Sprites in a vector!?STUCK
« on: January 29, 2014, 07:56:40 pm »
Thanks Azaral I finally got it working using your index method. Sprite and texture are no longer pointers. I guess the iterator method is just not for me.


 for (unsigned int i = 0; i < SpriteVector.size(); i++)
        {
            if (SpriteVector[i].getPosition().y <= 600)
        {
        SpriteVector[i].move(5, 5);
        }
        }

        //RENDERING
        window.clear();
        for (unsigned int i = 0; i < SpriteVector.size(); i++)
        {
        if (SpriteVector[i].getPosition().y <= 600)
        {
         window.draw(SpriteVector[i]);
        }
        else if (SpriteVector[i].getPosition().y >= 601)
        {
         SpriteVector.erase(SpriteVector.begin() + i); //this succesfully removes the object from the vector
         std::cout << "Container size..." << SpriteVector.size() << "\n";
        }
        }

 

Thanks again Azaral

2
Graphics / Re: Deleting Sprites in a vector!?STUCK
« on: January 29, 2014, 06:46:08 pm »
Ive tired clear() and I get the same error. Do I need to stop iterating through the vector before I can call clear/erase/ or delete?

3
Graphics / Re: Deleting Sprites in a vector!?STUCK
« on: January 29, 2014, 06:31:26 pm »
Thanks for the reply! Having a container with objects already in it and only adding to the container if all the objects are being used is the way to go here, smh I never thought of that!

I was using pointers just to try to implement the code a different way to see if it worked but your right they are not needed here.

Now, let say I want to reset these vectors to their original size once a new level starts, what would be the correct way so that I do not get a BAD_ACCESS error? What way do you use to delete items in a vector?

4
Graphics / Deleting Sprites in a vector!?STUCK
« on: January 29, 2014, 05:28:16 pm »
Hello SFML community,

I'm using SFML 2.1 with xcode 5 on my macbook air and I am somewhat new to SFML & C++(I have a couple of months under my belt).  I am in need of some help when it comes to Sprites & memory management (EXAMPLE: removing bullets when they go off screen, etc). I am creating a platform game and I have been stuck for weeks trying to figure this out. I have looked on a good amount of forums regarding this issue and I've come across examples where erase(it) being used inside of a for loop using an iterator. I've tried this but it does not work for me.

I would like to know how to clear out a vector that contains Sprites (or class objects w/ Sprites) using an iterator. And I would like these Sprites to be cleared out based on a boolean value. Which ever way I try to implement this I get an BAD_ACCESS error or the program crashes after the Sprite or object is removed from the vector using erase(). I have tried ever way I can think of but nothing works.

Instead of posting my game which contains hundreds of lines of code I wrote a simpler code, please examine it and help me understand what I am doing wrong. :(


#include <SFML/Graphics.hpp>
//#include "ResourcePath.hpp"
#include <iostream>
#include <vector>





int main()
{

    bool is_Alive;
    is_Alive = true;
    sf::Sprite* Sprite;
    Sprite = new sf::Sprite;

    std::vector<sf::Sprite> SpriteVector;
    typedef std::vector<sf::Sprite>::iterator iter;

    //loading projectile image
    sf::Texture* projectileImage;
    projectileImage = new sf::Texture;
    projectileImage->loadFromFile("fireball.png");

    Sprite->setTexture(*projectileImage);


    //Creating game window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Game");

    //set famerate to 60 FPS
    window.setFramerateLimit(60);

    window.setKeyRepeatEnabled(false);

    sf::Event event;

    float moveSpeed = 0;

    //variable that keeps game loop running
    bool play = true;
    //Game Loop
    while (play == true)
    {

        //Events

        while (window.pollEvent(event))
        {

            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
            {
                SpriteVector.push_back(*Sprite);
            }




            if (event.type == sf::Event::Closed)
           {

           }


       }

        //LOGIC-----------------------------------------------------------------------------------------------------

        std::cout << "SpriteVector is..." << SpriteVector.size() << "\n";

        for (iter it = SpriteVector.begin(); it != SpriteVector.end(); it++)
        {
            if ((it)->getPosition().y <= 600)
        {
        (it)->move(5, 5);
        }
        }

        //RENDERING
        window.clear();
        for (iter it = SpriteVector.begin(); it != SpriteVector.end(); it++)
        {
        if ((it)->getPosition().y <= 600)
        {
         window.draw(*it);
        }
        if ((it)->getPosition().y >= 601)
        {
         SpriteVector.erase(it);
         std::cout << "Erasing..." << (&it) << "Container size..." << SpriteVector.size() << "\n";
        }
        }
        window.display();
    }

    //Clean Up
    window.close();
    return 0;
}

 

Pages: [1]