[HELP] What am i doing wrong (GetPlayerKeys)
#1

i was just testing round with it but i cant seem to get it to work
i've looked at the wiki page for it

well heres the code
Код:
forward Jump(playerid);
Код:
public Jump(playerid)
{
	new keys, updown, leftright;
	GetPlayerKeys(playerid, keys, updown, leftright);
  if(keys == KEY_JUMP)
  {
  SendClientMessage(playerid,0xFFFFFFFF,"JUMP has been pressed");
	}
	return 1;
}
it compiles no errors i just dont know why it doesnt work
Reply
#2

Use SetTimer, or OnPlayerUpdate, example:

pawn Код:
public OnPlayerUpdate(playerid)
{
    new keys, updown, leftright;
    GetPlayerKeys(playerid, keys, updown, leftright);
    if(keys == KEY_JUMP)
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "JUMP has been pressed");
    }
    return 1;
}
And, btw, you posted in the wrong section.
Reply
#3

Quote:
Originally Posted by Toribio

And, btw, you posted in the wrong section.
Ohh sorry

Thanks
Reply
#4

All is much easier
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys - oldkeys == KEY_JUMP)
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "JUMP has been pressed");
    }
    return 1;
}
Reply
#5

The keys are a set of bits. Multiple keys/buttons can be pressed at the same time. So rather than using == or - you should use bitwise &.

pawn Код:
if(keys == KEY_JUMP) { ..
That detects if only the jump key is pressed and no other keys. For example, that condition would be false if the player pressed both KEY_SPRINT and KEY_JUMP at the same time.

pawn Код:
if(keys & KEY_JUMP) { ..
That detects if the KEY_JUMP key is pressed in combination with any other key.

pawn Код:
IsKeyJustDown(key, newkeys, oldkeys)
{
    if((newkeys & key) && !(oldkeys & key)) return 1;
    return 0;
}
Reply
#6

if((newkeys & key) && !(oldkeys & key)) and if(newkeys - oldkeys == KEY_JUMP) Same

If the player has clamped KEY_SPRINT (8 ) and at this time has pressed KEY_JUMP (32) in public will return:
newkeys = KEY_SPRINT (8 ) + KEY_JUMP (32) = 40
oldkeys = KEY_SPRINT (8 )
newkeys - oldkeys = 40 - KEY_SPRINT (8 ) = KEY_JUMP (32) - Key pressing
oldkeys - newkeys = 40 - KEY_SPRINT (8 ) = KEY_JUMP (32) - Key unpressing

All is considered
Reply
#7

Quote:
Originally Posted by stepmex
All is considered
Not really because you're assuming the KEY_SPRINT was pressed before KEY_JUMP, or any other key for that matter. What happens if they're both pressed at the same time or a new key is added in to the equation?

newkeys = 40 (KEY_JUMP | KEY_SPRINT)
oldkeys = 0

newkeys - oldkeys = 40. if (40 == KEY_JUMP) is false. Now you miss the condition to detect it being pressed.

When dealing with bits you use bitwise & (AND) or | (OR). If you use arithmetic you won't be testing individual bits but combinations of bits.
Reply
#8

It is impossible to press two buttons simultaneously, It is improbable
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)