Detecting updown/leftright keys
#6

pawn Code:
new pKey[MAX_PLAYERS][4]; // 0=up 1=down 2=left 3=right

public OnPlayerUpdate(playerid)
{
    new keys, updown, leftright;
    GetPlayerKeys(playerid, keys, updown, leftright);

    if(updown == KEY_UP && !pKey[playerid][0])
    {
        pKey[playerid][0] = 1; // Remember that they are pressing up
        OnPlayerUDLR(playerid, 0); // Call the function
    }
    if(updown != KEY_UP && pKey[playerid][0]) pKey[playerid][0] = 0; // Up no longer pressed

    if(updown == KEY_DOWN && !pKey[playerid][1])
    {
        pKey[playerid][1] = 1; // Remember that they are pressing down
        OnPlayerUDLR(playerid, 1); // Call the function
    }
    if(updown!= KEY_DOWN && pKey[playerid][1]) pKey[playerid][1] = 0; // Down no longer pressed

    if(leftright == KEY_LEFT && !pKey[playerid][2])
    {
        pKey[playerid][2] = 1; // Remember that they are pressing left
        OnPlayerUDLR(playerid, 2); // Call the function
    }
    if(leftright != KEY_LEFT && pKey[playerid][2]) pKey[playerid][2] = 0; // Left no longer pressed

    if(leftright == KEY_RIGHT && !pKey[playerid][3])
    {
        pKey[playerid][3] = 1; // Remember that they are pressing right
        OnPlayerUDLR(playerid, 3); // Call the function
    }
    if(leftright != KEY_RIGHT && pKey[playerid][3]) pKey[playerid][3] = 0; // Right no longer pressed
    return 1;
}
pawn Code:
OnPlayerUDLR(playerid, udlr)
{
    switch(udlr)
    {
        case 0:
        {
            // pressed up
        }
        case 1:
        {
            // pressed down
        }
        case 2:
        {
            // pressed left
        }
        case 3:
        {
            // pressed right
        }
    }
    return 1;
}
Un-tested and rushed.
Reply


Messages In This Thread
Detecting updown/leftright keys - by oliverrud - 16.11.2011, 16:29
Re: Detecting updown/leftright keys - by Biesmen - 16.11.2011, 17:23
Re: Detecting updown/leftright keys - by oliverrud - 16.11.2011, 17:36
Re: Detecting updown/leftright keys - by SmiT - 16.11.2011, 17:45
Re: Detecting updown/leftright keys - by oliverrud - 16.11.2011, 17:46
Re: Detecting updown/leftright keys - by MP2 - 16.11.2011, 18:04

Forum Jump:


Users browsing this thread: 1 Guest(s)