if (aCHAR == 'character' || 'another character') problems
Hi so i'm trying to check a certain character in a string to make sure it's not a \,=,|, etc. and replacing the space with the "player" character if it's not but the function is returning true everytime, even if char newLoc is equal to '' (empty):
screen.get_contents returns a vector container full of strins, and
sprite.get_location returns an int array with two number, [0] representing X, [1] is Y.bool check_collision(Sprite& sprite,int X, int Y, Screen& screen)
{
////////////////////// check whats alrea开发者_如何学Cdy there /////
char newLoc = screen.get_contents(sprite.get_location()[0]+Y,sprite.get_location()[1]+X);
if (newLoc == '|' || '/' || '_' || '=' || 'X' || 'x' )
return true;
else
return false;
};
what is the problem? Thanks!!
You need:
if (newLoc == '|' || newLoc == '/' || ...)
What you have written is equivalent to:
if (newLoc == ('|' || '/' || ...))
which is equivalent to:
if (newLoc == 1)
Note that a cleaner way of writing this might be:
switch (newLoc)
{
case '|':
case '/':
...
return true;
default:
return false;
}
newLoc == '|' || '/' || '_' || '=' || 'X' || 'x'
doesn't work, you must do it like this:
newloc == '|' || newloc == '/' || etc...
However this is more easy to read:
switch (newloc):
case '|':
case '/':
case '_':
case '=':
case 'X':
case 'x':
return true;
default:
return false;
精彩评论