For loop Vector size not working?

Started by
1 comment, last by FLeBlanc 11 years, 10 months ago
Hey there guys, I'm having a bit of a problem here, so far Beernutts has helped me with collision detection but now I'm facing a new error, I'm using SDL and C++ to create a simple game, each block has a X,Y width and height, this is passed into the hero class into a vector, then in the player class the vector(which stores all of the SDL_Rects for each block on the map) stored is then used to check for collisions, but when it checks it only checks the first one, any idea?

here is some of my code

Main.cpp

std::vector<block> blocks;
blockOne.setPosition(0.0f,400.0f); blocks.push_back(blockOne);
blockOne.setPosition(32.0f,400.0f); blocks.push_back(blockOne);
blockOne.setPosition(64,400); blocks.push_back(blockOne);
blockOne.setPosition(96,400); blocks.push_back(blockOne);
blockOne.setPosition(128,400); blocks.push_back(blockOne);
blockOne.setPosition(160,400); blocks.push_back(blockOne);
//blockOne.setPosition(192,400); blocks.push_back(blockOne);
blockOne.setPosition(0,368); blocks.push_back(blockOne);
blockOne.setPosition(0,336); blocks.push_back(blockOne);
for(int i=0;i<blocks.size();i++)
{
player.takeRectCollision(blocks.at(i).rtnCollision(),blocks.at(i).rtnID());
}

sets up some blocks, and then calls the player's class to store the SDL_Rect,

Block.cpp

SDL_Rect block::rtnCollision()
{
SDL_Rect temp;
temp.x = pos.x;
temp.y = pos.y;
temp.w = width;
temp.h = height;
return temp;
}


Player.cpp

if(collision.vectorCheckCollision(rtnCollision(),rects))
{
pos.x -= xVel;
xVel = 0.0f;
}
SDL_Rect hero::rtnCollision()
{
SDL_Rect temp;
temp.x = pos.x;
temp.y = pos.y;
temp.w = width;
temp.h = height;
return temp;
}
void hero::takeRectCollision(SDL_Rect &rectTake, int ID)
{
rects.push_back(rectTake);
std::cout<<rects.size()<<std::endl;
rectIDs.push_back(ID);
}


this is not the whole code for each class, just the coded needed

gMaths.cpp

bool vectorCheckCollision(SDL_Rect obj1, std::vector<SDL_Rect> vecRects)
{
for(int i = 0;i<vecRects.size();i++)
{
SDL_Rect B; // block/monster/whatever
B.x = vecRects.at(i).x;
B.y = vecRects.at(i).y;
B.w = vecRects.at(i).w;
B.h = vecRects.at(i).h;
if(check_collision(obj1,B))
{
return true;
}
else
{
return false;
}
}
}


now in the for loop above, it only checks the first block, it doesn't go through the whole vector list, can anyone help me?

Thank you so much

P.S. also i did debug the program, vecRects is equal to 8, so the blocks are in there
Advertisement
Its ok, with a simple fix of the for loop i have it working,

in this code here

bool vectorCheckCollision(SDL_Rect obj1, std::vector<SDL_Rect> vecRects)
{
for(int i = 0;i<vecRects.size();i++)
{
SDL_Rect B; // block/monster/whatever
B.x = vecRects.at(i).x;
B.y = vecRects.at(i).y;
B.w = vecRects.at(i).w;
B.h = vecRects.at(i).h;
if(check_collision(obj1,B))
{
return true;
}
else
{
return false;
}
}
}


when a return statement is called, it breaks the method and just continues the rest of the code and forgets the while, for whatever state.
so with a simple boolean variable it now works


bool vectorCheckCollision(SDL_Rect obj1, std::vector<SDL_Rect> vecRects)
{
bool returnValue = false;
for(int i = 0;i<vecRects.size();i++)
{
SDL_Rect B; // block/monster/whatever
B.x = vecRects.at(i).x;
B.y = vecRects.at(i).y;
B.w = vecRects.at(i).w;
B.h = vecRects.at(i).h;
if(check_collision(obj1,B))
{
returnValue = true;
}
}
return returnValue;
}


Topic can be deleted :)
It only checks the first one, because you are returning either true or false depending on the return value of check_collision. If there is a collision, you return true, otherwise you return false. The rest of the loop is never even given a chance to execute.

Edit: Doh, ninja edit while typing. Glad you got it working.

This topic is closed to new replies.

Advertisement