samedi 18 avril 2015

If..else if..else statement always going into the else

After tracking the running program with the debugger I've determined that the following code always goes into the 'else' statement. The 'if' and 'if..else' are working fine and I can't see anything that would cause the code to always go into the 'else' I'm using the free Student version of VS 2010 Professional.



void Worm::update(Item &apple)
{
// local variables
char dir;
sf::Vector2f pos;
sf::Vector2f applePos = apple.getPosition();
sf::Vector3i colour;
std::vector<WormCell>::iterator it;

for (it = cells.begin() ; it != cells.end(); it++)
{
// local variables
dir = it->getDirection();
pos = it->getPosition();

// Pre conditions for if
float xx = applePos.x - pos.x;
float yy = applePos.y - pos.y;

if(it == cells.begin() && dir == 's' && xx == 0 && yy == rect)
{
// Add apple to worm
colour = sf::Vector3i(255, 255, 0);
it = cells.insert ( it , (WormCell(colour, applePos, dir)));
apple.setShow(false);
}
else if(it == cells.begin() && dir == 'n' && xx == 0 && yy == -1 * rect)
{
// Add apple to worm
colour = sf::Vector3i(0, 255, 255);
it = cells.insert ( it , (WormCell(colour, applePos, dir)));
apple.setShow(false);
}
else if (it == cells.begin() && dir == 'e' && yy == 0 && xx == rect)
{
// Add apple to worm
colour = sf::Vector3i(0, 0, 255);
it = cells.insert ( it , (WormCell(colour, applePos, dir)));
apple.setShow(false);
}
else if (it == cells.begin() && dir == 'w' && yy == 0 && xx == -1 * rect)
{
// Add apple to worm
colour = sf::Vector3i(255, 0, 255);
it = cells.insert ( it , (WormCell(colour, applePos, dir)));
apple.setShow(false);
}
else
{
// Move worm
moveWorm(dir, pos);

// set new cell position
it->setPosition(pos);
}
}
// Shuffle directions
shuffle();


Can anyone tell me why it's doing that?


Aucun commentaire:

Enregistrer un commentaire