Quote:Original post by siliconsurfer
Ok cheers, does direction = S compare ASCII values? If not how does do the comparison between differnt letters?
Cheers
The
single quotes are important, but yes.
A char variable stores a
single value; it is not a "string" even in the C sense. C-style "strings" are dangerous hacks that assume that the character pointed to
and the bits of data that follow it in memory are all character-typed data which eventually end with a \0.
By the way, this:
theSprite->setY(theSprite->getY()+10);
is AWFUL.
The sprite itself should be handling this:
void Sprite::moveSouth(int distance) { y += distance; // Any other checking logic that used to be in the setter goes here.}
Then you can expose an interface with methods that
do something and have a
meaningful name. If you simply do everything through accessors and mutators then you are lying to yourself about encapsulation.
theSprite->moveSouth(10);
Later, we might change this to be a single 'move' method which accepts the direction as a parameter (either as a char or as the suggested enumeration value). Then we can directly do:
theSprite->move(10, badguy->getDirection());
Except that of course, we learn from the previous discussion, and avoid the accessor. The enemy object really should be more closely associated with its state anyway:
void enemy::move() { mySprite->move(10, myDirection);}// where mySprite is a member variable of the enemy class...badguy->move();
OO isn't about keeping data out of the wrong places; it is about putting code in the right places.