• having some issues with this project (C++)
    5 replies, posted
having some problems with this code (getting really mixed up with these vectors). i'm pretty new to C++ so help would be appreciated, i've been stuck on this for a while. i'm using visual studio 2012 if that's important. [CODE] void Player::Update(int a_iScreenWidth, int a_iScreenHeight, unsigned int a_uiDefaultTexture, unsigned int a_uiPlayerShooting, vector <Enemy*> a_Enemies) { Control(a_iScreenWidth, a_iScreenHeight, a_uiDefaultTexture, a_uiPlayerShooting); MoveSprite(m_uiTexture, m_oPosition.x, m_oPosition.y); Draw(); //Updates bullets based on how many are in the bullet vector. Bullet's are added to the vector when space is pressed. If no bullets //are present in the vector this code is skipped. If bullets are present in the vector, this code updates the bullet's each frame //and removes the bullet from the vector if they reach the edge of the screen. for(int i = 0; i < m_oPlayerBullets.size(); i++) { if(a_Enemies.empty() == false) { for (int j = 0; j < a_Enemies.size(); j++) { m_oPlayerBullets[i]->CheckCollision(a_Enemies[j]->getX(), a_Enemies[j]->getWidth()); } } if( m_oPlayerBullets[i]->getX() > a_iScreenWidth || m_oPlayerBullets[i]->getHit() == true) { auto iter = m_oPlayerBullets.begin(); iter = iter + i; iter = m_oPlayerBullets.erase(iter2); } m_oPlayerBullets[i]->Update(); } } [/CODE] this is for an assignment (this is my first assignment so sorry if this is hard to read/understand), it's a scrolling shooter and this is a function in my player class. the bullets were shooting properly before, they move to the right when they're created and pressing spacebar initializes them with a position based on the player's position. i also successfully got them to erase from the vector when they hit the edge of the screen, but what i'm trying to do now is have the bullet erase itself from the vector when it collides with an enemy. the enemies are also a vector that work similarly, i have them move to the left as soon as they're created, spawn slightly off screen on the x axis and on a random position on the y axis between the bottom and top of the screen. i'm trying to pass in the vector of enemies into this update function. "check collision" is a function in the bullet that sets a bool to true if the bullets X position + its width is equal to the enemys X position. i added a for loop that checks the collision of each bullet and each enemy and added an extra check to the if statement that erases the bullets from the vector if they're offscreen, that checks if the bool is true. every time i press spacebar i get this error: [img]http://i58.tinypic.com/wsjvgi.png[/img] what's the issue with this? it seems to me like this should work and i don't know why it wouldn't/
What is iter2 ?
that was just a typo, i fixed that (and it still doesn't run)
what line is the 1140? Well, I believe the problem is when you erase a bullet, then you still call ->Update(); try [CODE] if( m_oPlayerBullets[i]->getX() > a_iScreenWidth || m_oPlayerBullets[i]->getHit() == true) { auto iter = m_oPlayerBullets.begin(); iter = iter + i; iter = m_oPlayerBullets.erase(iter2); } else { m_oPlayerBullets[i]->Update(); }[/CODE]
[cpp]void Player::Update(int a_iScreenWidth, int a_iScreenHeight, unsigned int a_uiDefaultTexture, unsigned int a_uiPlayerShooting, vector <Enemy*> a_Enemies) { Control(a_iScreenWidth, a_iScreenHeight, a_uiDefaultTexture, a_uiPlayerShooting); MoveSprite(m_uiTexture, m_oPosition.x, m_oPosition.y); Draw(); //Updates bullets based on how many are in the bullet vector. Bullet's are added to the vector when space is pressed. If no bullets //are present in the vector this code is skipped. If bullets are present in the vector, this code updates the bullet's each frame //and removes the bullet from the vector if they reach the edge of the screen. for(int i = 0; i < m_oPlayerBullets.size(); i++) { if(a_Enemies.empty() == false) { for (int j = 0; j < a_Enemies.size(); j++) { m_oPlayerBullets[i]->CheckCollision(a_Enemies[j]->getX(), a_Enemies[j]->getWidth()); } } if( m_oPlayerBullets[i]->getX() > a_iScreenWidth || m_oPlayerBullets[i]->getHit() == true) { auto iter = m_oPlayerBullets.begin(); iter = iter + i; iter = m_oPlayerBullets.erase(iter); i--; // < This is so we don't accidentally skip over an element. continue; // < This will fix your problem. } m_oPlayerBullets[i]->Update(); // < This is where the problem happens. } }[/cpp] Notice the continue. The problem was that you were deleting a bullet and then trying reference it immediately after. Also since the entire vector was shifted down one because of the erase, I'm pretty sure you need to decrement i to make sure it gets all of the elements.
working now, thanks a lot guys
Sorry, you need to Log In to post a reply to this thread.