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