dev.gamez.lv Forum Index dev.gamez.lv
Latvian Game Developers Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 

Klaviatūras input

 
dev.gamez.lv Forum Index -> Iesācējiem
View previous topic :: View next topic  
Author Message
Tankists



Joined: 18 Dec 2006
Posts: 81
Location: Baldone

PostPosted: Sat Aug 30, 2008 7:09 pm    Post subject: Klaviatūras input

Šobrīd esmu uztaisijis nelielu aplikāciju ar SDL, kurā tanciņš braukā pa tailu karti. Problēma sākās tajā, ka lietotājs nospiežot būltiņu uz augšu un uz sānu, tanciņš brauc slīpi, bet man nepieciešams lai tiktu ņemts vērā tikai viens klaviatūras taustiņš(lai nevarētu tanciņu pārvietot slīpi). Kā to panākt?

Mana tanciņa handle_input() f-cija:
Code:
void Tank::handle_input()
{
    if (event.type == SDL_KEYDOWN)
    {
        switch (event.key.keysym.sym)
        {
            case SDLK_UP: yVel -= TILE_SIZE / 4; turned = TANK_UP; break;
            case SDLK_DOWN: yVel += TILE_SIZE / 4; turned = TANK_DOWN; break;
            case SDLK_LEFT: xVel -= TILE_SIZE / 4; turned = TANK_LEFT; break;
            case SDLK_RIGHT: xVel += TILE_SIZE / 4; turned = TANK_RIGHT; break;
        }
    }
    else if (event.type == SDL_KEYUP)
    {
        switch (event.key.keysym.sym)
        {
            case SDLK_UP: yVel += TILE_SIZE / 4; break;
            case SDLK_DOWN: yVel -= TILE_SIZE / 4; break;
            case SDLK_LEFT: xVel += TILE_SIZE / 4; break;
            case SDLK_RIGHT: xVel -= TILE_SIZE / 4; break;
        }
    }
}
Back to top
View user's profile Visit poster's website
bubu
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 23 Mar 2004
Posts: 3223
Location: Riga

PostPosted: Sat Aug 30, 2008 7:43 pm    Post subject:

Code:
if (nospiests taustiņš)
{
    if (uz augšu)
    {
        yVel = -Y;
        xVel = 0;
    }
    else if (pa labi)
    {
        yVel = 0;
        xVel = +X;
    }
    ...
}
else if (atlaists taustiņš)
{
    xVel = 0;
    yVel = 0;
}


Jeb alternatīvi xVel/yVel vietā glabā tikai vienu virziena enumu (MOVE_STOP, MOVE_UP, MOVE_LEFT, MOVE_DOWN, MOVE_RIGHT) un šajā if'ā uzstādi tikai vienu no enum'a vērtībām.
Un tai vietā, kur vajag mainīt koordinātes, tur attiecīgi paskaties šī enuma vērtību.


Last edited by bubu on Sat Aug 30, 2008 7:45 pm; edited 1 time in total
Back to top
View user's profile Send e-mail
Tups



Joined: 16 Jul 2008
Posts: 50

PostPosted: Sat Aug 30, 2008 7:44 pm    Post subject:

Nu nezinu vai šitas atrisinās tavu problēmu, bet vari jau pameigināt:


Code:
bool VaiIrNospiestaCitaPoga = false;
void Tank::handle_input()
{
    if (event.type == SDL_KEYDOWN)
    {
      if(event.key.state
        switch (event.key.keysym.sym)
        { 
               if(VaiIrNospiestaCitaPoga == false){
                    case SDLK_UP: yVel -= TILE_SIZE / 4; turned = TANK_UP; break;
                     VaiIrNospiestaCitaPoga = true;
               }
               if(VaiIrNospiestaCitaPoga == false){
                    case SDLK_DOWN: yVel += TILE_SIZE / 4; turned = TANK_DOWN; break;
                     VaiIrNospiestaCitaPoga = true;
               }
               if(VaiIrNospiestaCitaPoga == false){
                    case SDLK_LEFT: xVel -= TILE_SIZE / 4; turned = TANK_LEFT; break;
                     VaiIrNospiestaCitaPoga = true;
               }
               if(VaiIrNospiestaCitaPoga == false){
                   case SDLK_RIGHT: xVel += TILE_SIZE / 4; turned = TANK_RIGHT; break;
                     VaiIrNospiestaCitaPoga = true;
              }
        }
    }
    else if (event.type == SDL_KEYUP)
    {
        switch (event.key.keysym.sym)
        {
            case SDLK_UP: yVel += TILE_SIZE / 4; break;
              VaiIrNospiestaCitaPoga = false;
            case SDLK_DOWN: yVel -= TILE_SIZE / 4; break;
              VaiIrNospiestaCitaPoga = false;
            case SDLK_LEFT: xVel += TILE_SIZE / 4; break;
              VaiIrNospiestaCitaPoga = false;
            case SDLK_RIGHT: xVel -= TILE_SIZE / 4; break;
              VaiIrNospiestaCitaPoga = false;
        }
    }
}
Back to top
View user's profile
Tankists



Joined: 18 Dec 2006
Posts: 81
Location: Baldone

PostPosted: Sat Aug 30, 2008 8:00 pm    Post subject:

Paldies jums. bubu variants likās sirdij tīkamāks un ērtāks. Wink
Back to top
View user's profile Visit poster's website
Tups



Joined: 16 Jul 2008
Posts: 50

PostPosted: Sat Aug 30, 2008 8:05 pm    Post subject:

Jā, godīgi sakot man ar tā šķiet.



.
Back to top
View user's profile
bubu
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 23 Mar 2004
Posts: 3223
Location: Riga

PostPosted: Sat Aug 30, 2008 8:40 pm    Post subject:

Tups: tavs kods vispār ir diezgan līks - case elementiem jābūt tieši zem switch. Tur nekādi if'i pa visam nedrīkst būt. Kā arī pēc break'a tālāk esošais kods nemaz neizpildās. break pārtrauc koda izpildīšanu konkrētajā case gabalā (augstāk esošajos koda piemēros).
Back to top
View user's profile Send e-mail
Tups



Joined: 16 Jul 2008
Posts: 50

PostPosted: Sat Aug 30, 2008 9:38 pm    Post subject:

Ā, nu jā, pareizi. Nu vismaz vel daži iemesli, kāpēc tavs padoms ir labāks. :)




.
Back to top
View user's profile
Display posts from previous:   
dev.gamez.lv Forum Index -> Iesācējiem All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group