Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (EternallyCurious, Quad, vicknick), 700 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
sprint code behaving irregularly. #235710
11/09/08 17:01
11/09/08 17:01
Joined: Oct 2008
Posts: 218
Nashua NH
heinekenbottle Offline OP
Member
heinekenbottle  Offline OP
Member

Joined: Oct 2008
Posts: 218
Nashua NH
I have a code that is suppose to make the character sprint much like in Half-Life 2. You press [shift] and while you press shift a counter counts to 300 and player speed is increased. When it hits 300, player speed is reset to 10. The counter begins to count down as soon as [shift] is released. When the counter hits 0 again, the player is able to sprint again.

The code sort of works, the problem is, the counter is behaving odd. Sometimes it counts too fast, usually when I'm moving, which means I can't run for the full 300 count. It ranges from 1-5 seconds. I need it to be constant.

Code:
var sprinting;			//a counter that increases as Suzetta sprints
var tired = 0;			//toggles when sprinting hits max

function sprint()
{
	if(key_shift)	//if shift is pressed
	{
		if(tired == 0)	//if tired is zero
		{
			if(sprinting < 300)	//if the sprinting variable is less than three hundred
			{
				sprinting += (sprinting < 300);	//increase by 1 if sprinting is less than max, constrained to max
				player.speed = 60;					//set player speed to 60
			}
			else						//if sprinting variable is greater/equal to than 300
			{
				tired = 1;				//set tired to 1
				player.speed = 10;	//set player speed back to 10
			}
		}	
		else
		{
			sprinting -= (sprinting != 0);	//reduce sprinting by 1 if sprinting isn't 0
			tired = (sprinting != 0);			//set tired to 0 when sprinting hits 0
		}
	}
	else										//if shift is released, allow sprinting to reduce back to 0
	{		
		sprinting -= (sprinting != 0);	//reduce sprinting by 1 if sprinting isn't 0
		tired = (sprinting != 0);			//set tired to 0 when sprinting hits 0
		player.speed = 10;					//set speed back to 10
	}

}


it is called in the player action, at the bottom of the rest of the movement script.


I was once Anonymous_Alcoholic.

Code Breakpoint;
Re: sprint code behaving irregularly. [Re: heinekenbottle] #235728
11/09/08 18:51
11/09/08 18:51
Joined: Oct 2004
Posts: 1,655
T
testDummy Offline
Serious User
testDummy  Offline
Serious User
T

Joined: Oct 2004
Posts: 1,655
Code:
// factor in time (time_step), was using frame
// expect faults
// just a guess
var sprinting = 0;  // seconds sprinting
var tired = 0;  // at sprinting == 300, then sprinting > 0
function sprint() {
	if (key_shift && !tired) {
		// if moving?
		sprinting += time_step * 0.0625;
		sprinting = minv(sprinting, 300);
		tired = (sprinting >= 300);
		player.speed = 60;
	} else {
		player.speed = 10;
		if (tired && !key_shift) { // stop pressing key_shift
			sprinting -= time_step * 0.0625;
			sprinting = maxv(sprinting, 0);
			tired = (sprinting >= 0);
		}
	}
}


Re: sprint code behaving irregularly. [Re: testDummy] #235865
11/10/08 16:19
11/10/08 16:19
Joined: Oct 2008
Posts: 218
Nashua NH
heinekenbottle Offline OP
Member
heinekenbottle  Offline OP
Member

Joined: Oct 2008
Posts: 218
Nashua NH
Although your script didn't work, the "time_step * 0.0625" part helped immensely and fixed the problem. I reduced the max to 15 (300 ticks was fine but not 300 seconds grin ) and now my character runs for a constant maximum of 15 seconds (give or take a thousandth of a second) and must rest afterward for a constant 15 seconds.

If she doesn't run the full 15 seconds, she still must rest for the time she did run if shift is released, still debating whether I want this effect.

So thanks

Code:

function sprint()		//allows the player to sprint for a maximum of 15 seconds, after which she must rest for the time she sprinted
{
	if(key_shift)					//if shift is pressed
	{
		if(tired == 0)				//if tired is zero
		{
			if(sprinting < 15)	//if the sprinting variable is less than max
			{
				sprinting += time_step * 0.0625;	//increase by 1 if sprinting is less than max, constrained to max
				player.speed = 60;					//set player speed to 60
			}
			else							//if sprinting variable is greater/equal to than max
			{
				tired = 1;				//set tired to 1
				player.speed = 10;	//set player speed back to 10
			}
		}	
		else
		{		
			sprinting -= (time_step * 0.0625) * (sprinting >= 0);	//reduce sprinting by 1 if sprinting isn't 0
			tired = (sprinting >= 0);										//set tired to 0 when sprinting hits 0
		}
	}
	else										//if shift is released, allow sprinting to reduce back to 0
	{		
		sprinting -= (time_step * 0.0625) * (sprinting >= 0);	//reduce sprinting by 1 if sprinting isn't 0
		tired = (sprinting >= 0);										//set tired to 0 when sprinting hits 0
		player.speed = 10;												//set speed back to 10
	}
}



I was once Anonymous_Alcoholic.

Code Breakpoint;

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1