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

Author Topic: function gives a vector out of range error.  (Read 5672 times)

0 Members and 1 Guest are viewing this topic.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
function gives a vector out of range error.
« on: December 01, 2013, 03:14:56 am »
I have a vecotr of bullets in my player class. A bullet is created when the user clicks the spacebar. When it's created, I have two loops that call the bullet's Update() and Draw() function, respectively. In the update function there is a function that updates the sprite's animation.

I'm using the SFML Animation classes (https://github.com/SFML/SFML/wiki/Source%3A-AnimatedSprite)

Here is the code that's giving me a problem:

bullet.cpp:
void Bullet::Update()
{
        Move();

        sprite.update(frameClock.restart());
}
+

and in the player.cpp:

void Player::Move()
{
       
        for(int i = 0; i < bullets.size(); i++)
        {
                bullets[i].Update();
        }
       

        sprite.move(0, velocity.y);
}

Everything works UNTIL I PUT IN THE
sprite.update(frameClock.restart());
IT GIVES THE ERROR.

Does anyone know why?

edit:
here's the sprite.Update() source code from git:
oid AnimatedSprite::update(sf::Time deltaTime)
{
    // if not paused and we have a valid animation
    if(!m_isPaused && m_animation)
    {
        // add delta time
        m_currentTime += deltaTime;

        // if current time is bigger then the frame time advance one frame
        if(m_currentTime >= m_frameTime)
        {
            // reset time, but keep the remainder
            m_currentTime = sf::microseconds(m_currentTime.asMicroseconds() % m_frameTime.asMicroseconds());

            // get next Frame index
            if(m_currentFrame + 1 < m_animation->getSize())
                m_currentFrame++;
            else
            {
                // animation has ended
                m_currentFrame = 0; // reset to start

                if(!m_isLooped)
                {
                    m_isPaused = true;
                }

            }

            // set the current frame, not reseting the time
            setFrame(m_currentFrame, false);
        }
    }
}

Ditto

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: function gives a vector out of range error.
« Reply #1 on: December 01, 2013, 10:42:12 pm »
Could it possibly be that you haven't added any frames to the animation? I haven't used the animation classes you use but since the sprite.update() seems to modify the frames-vector it is likely to be the problem? Would you like to provide the code where you initialize your sprite?

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: function gives a vector out of range error.
« Reply #2 on: December 02, 2013, 06:11:18 am »
I've added the frames, but here's the sprite constructer!

Bullet::Bullet(sf::Texture& nTexture, sf::Vector2f nPos)
{
        movingAnimation.setSpriteSheet(nTexture);
        movingAnimation.addFrame(sf::IntRect(27, 134, 4, 9));
        movingAnimation.addFrame(sf::IntRect(35, 134, 4, 11));
        movingAnimation.addFrame(sf::IntRect(43, 134, 4, 13));
        movingAnimation.addFrame(sf::IntRect(50, 134, 6, 15));

        sprite.setPosition(nPos);
        sprite.setAnimation(movingAnimation);
        sprite.setFrameTime(sf::milliseconds(275));
        sprite.setLooped(true);
        sprite.setScale(2, 2);
        sprite.setRotation(90);

        speed = 4;
        velocity = sf::Vector2f(0 ,0);
}
 


Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: function gives a vector out of range error.
« Reply #3 on: December 02, 2013, 08:42:48 am »
Could you please be more specific as to what the error is? Which vector is causing this error? I suspect that it might be your bullets vector.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: function gives a vector out of range error.
« Reply #4 on: December 02, 2013, 11:24:23 am »
Sorry. Yes, it's the bullets vector. The error is in the title :)

"Expression: vector subscript out of range"


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: function gives a vector out of range error.
« Reply #5 on: December 02, 2013, 07:48:00 pm »
The error means you access an element with an invalid index (=subscript).

Run your debugger, go up the callstack until you find the vector::operator[] call in user code, look at the value of the index expression and see why it's wrong. If you had used the debugger, this problem would have been solved within minutes, and you wouldn't have had to write such a long post and wait one day for an answer.

A general advice: Don't use random access to iterate. It's possibly slower and makes it harder to switch containers. Instead, use iterators:
for (auto itr = bullets.begin(); itr != bullets.end(); ++itr)
    itr->Update();
Or directly the range-based for loop:
for (Bullet& bullet : bullets)
    bullet.Update();
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: function gives a vector out of range error.
« Reply #6 on: December 03, 2013, 05:27:35 am »
I have no idea how to use call stack. That's why I didn't use it.

I didn't want to write 'such a long post' but people need as much information to see my problem. Right?


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: function gives a vector out of range error.
« Reply #7 on: December 03, 2013, 07:18:02 am »
What I wanted to say is that you should learn using a debugger, it will save you a lot of time to fix things. The debugger is a tool every programmer should know...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: function gives a vector out of range error.
« Reply #8 on: December 03, 2013, 07:37:31 am »
You haven't given any relevant information relating to YOUR bullet vector, you've just given unrelated code. Show the code that is throwing the vector error, please.

Also, everything Nexus said. How do you not know about a debugger? A debugger would have helped you solve this problem immediately.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: function gives a vector out of range error.
« Reply #9 on: December 04, 2013, 04:46:00 am »
Because I don't know how to use it?  What kind of question is that..? I'm sorry I've never used one. I don't know why you two act like it's so profound for me not to know how to use one. Especially in the general forum. Last I check super noobie questions are asked all the time here.

I have shown code that throws the error, but here is it again.

player.cpp:

void Player::Move()
{
   
    for(int i = 0; i < bullets.size(); i++)
    {
        bullets[i].Update();
    }
   

    sprite.move(0, velocity.y);
}

That's the loop that goes through the bullets' update function. Here's what a bullet's Update function looks like:

void Bullet::Update()
{
    Move();

    sprite.update(frameClock.restart());
}

The Move() function alone doesn't throw an error, but when I put in:

 sprite.update(frameClock.restart());

the error comes.
« Last Edit: December 04, 2013, 04:53:12 am by asusralis »

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: function gives a vector out of range error.
« Reply #10 on: December 04, 2013, 04:49:49 am »
@Nexus I'll take your advice and do that.

 

anything