Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Puzzle 1 - Double Tap Running
#3
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:
    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~
Reply
Thanks given by: A-Man


Messages In This Thread
Puzzle 1 - Double Tap Running - by Azriel - 07-25-2013, 07:56 AM
RE: Puzzle 1 - Double Tap Running - by A-Man - 07-25-2013, 02:10 PM
RE: Puzzle 1 - Double Tap Running - by Azriel - 07-27-2013, 09:47 AM
RE: Puzzle 1 - Double Tap Running - by A-Man - 07-27-2013, 11:48 AM
RE: Puzzle 1 - Double Tap Running - by Azriel - 07-27-2013, 11:02 PM
RE: Puzzle 1 - Double Tap Running - by Som1Lse - 07-28-2013, 01:06 PM
RE: Puzzle 1 - Double Tap Running - by A-Man - 07-28-2013, 02:38 AM



Users browsing this thread: 1 Guest(s)