Help with if pressed and if released
#1

Hi i need make a timer when i holding the key the timer are true when i released the timer are false
my code:
Код:
if(HOLDING(KEY_SPRINT))
	{
  	SetTimer("GTSpeed", 1000, true);
return 1;
	}

if (RELEASED(KEY_SPRINT))
		{
			SetTimer("GTSpeed", 100000000, false);
		}
gtspeed public:
Код:
public GTSpeed(playerid)
	{
		    new Float:sXxx,Float:sYxx,Float:SZxx;
			GetVehicleVelocity( GetPlayerVehicleID( playerid ),sXxx,sYxx,SZxx);
			SetVehicleVelocity( GetPlayerVehicleID( playerid ),sXxx*1.1,sYxx*1.1,SZxx*1.1);
	    return 1;
	}
when i my timer starts it wont stop, my car going until 100000km/h speed i want to make when i'm holding key_sprint then my car every 1 second speed up :/ but i don't know how to make that, if anyone can help my i be really thankful.
Reply
#2

don't use "HOLDING" use "PRESS" instead... so the moment that you press the button for the first time, ane the repeating timer.. 'cause in your case it starts quite a lot timers that are repeating...

so like this:
pawn Код:
new acc_time[MAX_PLAYERS];
forward GTSpeed(playerid);

public GTSpeed(playerid)
{
    new Float:sXxx,Float:sYxx,Float:SZxx;
    GetVehicleVelocity( GetPlayerVehicleID( playerid ),sXxx,sYxx,SZxx);
    SetVehicleVelocity( GetPlayerVehicleID( playerid ),sXxx*1.1,sYxx*1.1,SZxx*1.1);
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
  if ((newkeys & KEY_SPRINT) && !(oldkeys & KEY_SPRINT))
  {
    acc_time = SetTimerEx("GTSpeed", 1000, true, "i", playerid);
    return 1;
  }
  if (!(newkeys & KEY_SPRINT) && (oldkeys & KEY_SPRINT))
  {
    KillTimer(acc_time[playerid]);
    return 1;
  }
  return 1;
}
*Note: not tested, neither in compiler nor in-game, so there could be typos, etc.
Reply
#3

Now you only need to add some security checks and done
pawn Код:
// global
new
    gSpeedTimer[MAX_PLAYERS] = {-1, ...}
;
// OnPlayerKeyStateChange
    if(PRESSED(KEY_SPRINT)) {
        if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
            gSpeedTimer[playerid] = SetTimerEx("GTSpeed", 1000, true, "i", GetPlayerVehicleID(playerid));
        }
    }
    if(RELEASED(KEY_SPRINT)) {
        if(gSpeedTimer[playerid] != -1) {
            KillTimer(gSpeedTimer[playerid]);
            gSpeedTimer[playerid] = -1;
        }
    }
// OnPlayerStateChange
    if(oldstate == PLAYER_STATE_DRIVER) {
        if(gSpeedTimer[playerid] != -1) {
            KillTimer(gSpeedTimer[playerid]);
            gSpeedTimer[playerid] = -1;
        }
    }
//
forward GTSpeed(vehicleid);
public GTSpeed(vehicleid) {
    const
        Float: multiplier = 1.1
    ;
    new
        Float: X,
        Float: Y,
        Float: Z
    ;
    GetVehicleVelocity(vehicleid, X, Y, Z);
    SetVehicleVelocity(vehicleid, X * multiplier, Y * multiplier, Z * multiplier);
}
Reply
#4

Thanks guys, works!!! :O
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)