-
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);
}
}
}
-
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?
-
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);
}
-
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.
-
Sorry. Yes, it's the bullets vector. The error is in the title :)
"Expression: vector subscript out of range"
-
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 (http://en.sfml-dev.org/forums/index.php?topic=10084) 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();
-
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?
-
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...
-
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.
-
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.
-
@Nexus I'll take your advice and do that.