07-27-2013, 09:47 AM
your code reminds me of how I used to write code before I learnt to structure code nicely (you'll learn in time, though you'd learn fastest when you code in pairs with someone who structures code nicely).
here's how I did mine:
Azriel~
here's how I did mine:
|
C++-Code:
/** * The number of frames elapsed since the last left/right control key press while this Character was in State::basic. * <p> * When the run counter is not started, its value is 0. * When the first left/right keypress is input, the counter value indicates number of frames that the key is pressed for. * When the left/right keypress is lifted, the counter value is negated if it is within the threshold, otherwise reset. * Subsequently, the counter value starts counting up. * When the next left/right keypress is input, if the counter has not yet reached 0, the character goes into the RUNNING sequence. */ int runCounter; // this is an extract of the real function void Character::doUpdate() { subtractFriction(); if (this->runCounter != 0) { ++this->runCounter; } bool movementKeyEnabled = false; if (this->inputState->isUpEnabled()) { movementKeyEnabled = true; this->vz = -this->moveSpeeds->getWalkZ(); selectSequence(WALKING); } else if (this->inputState->isDownEnabled()) { movementKeyEnabled = true; this->vz = this->moveSpeeds->getWalkZ(); selectSequence(WALKING); } if (this->inputState->isLeftEnabled()) { movementKeyEnabled = true; selectWalkOrRunSequence(false); } else if (this->inputState->isRightEnabled()) { movementKeyEnabled = true; selectWalkOrRunSequence(true); } else { // if we have started walking horizontally, but stopped walking horizontally, then we start counting "down" if (this->runCounter > 0) { this->runCounter = (this->runCounter <= RUN_COUNTER_THRESHOLD) ? -RUN_COUNTER_THRESHOLD : 0; } } if (!movementKeyEnabled) { selectSequence(STANDING); } } void Character::selectWalkOrRunSequence(const bool rightControlKeyPressed) { if ((this->facingRight == rightControlKeyPressed) && this->runCounter < 0) { this->vx = rightControlKeyPressed ? this->moveSpeeds->getRunX() : -this->moveSpeeds->getRunX(); selectSequence(RUNNING); this->runCounter = 0; } else { this->facingRight = rightControlKeyPressed; this->vx = rightControlKeyPressed ? this->moveSpeeds->getWalkX() : -this->moveSpeeds->getWalkX(); selectSequence(WALKING); // if the run counter was enabled for the opposite direction (-ve) or the character just started walking from standstill, we reset // the counter if (this->runCounter <= 0) { this->runCounter = 1; } } } |
Azriel~

Chat
![[Image: ZucdcMY.png]](https://i.imgur.com/ZucdcMY.png)