キー
Posted: 2008年1月25日(金) 11:24
if( DownOshita==1 || DownPusingCounter>30){ // たった今押したか、30カウンター以上押しっぱなしなら
if(DownPusingCounter>30) // 30カウンター以上押しっぱなしならカウンターを27へ
DownPusingCounter=27; // オートの場合4回に1回下に下がる。
#include "DxLib.h"
void char_disp(int White,int y){
DrawString( 150 , y , "■" , White );
DrawString( 170 , 100 , "NEW GAME" , White );
DrawString( 170 , 120 , "CONTINUE1" , White );
DrawString( 170 , 140 , "CONTINUE2" , White );
DrawString( 170 , 160 , "CONTINUE3" , White );
DrawString( 170 , 180 , "CONTINUE4" , White );
DrawString( 170 , 200 , "CONTINUE5" , White );
DrawString( 170 , 220 , "CONTINUE6" , White );
DrawString( 170 , 240 , "LOG OUT" , White );
}
void Key(void);
int White,y=100;
char KeyBuf[ 256 ] ;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE ) ;
if( DxLib_Init() == -1 ) return -1;
White = GetColor( 255 , 255 , 255 ) ;
SetDrawScreen( DX_SCREEN_BACK ) ;
while( 1 )
{
ClearDrawScreen();
GetHitKeyStateAll( KeyBuf ) ;
if( KeyBuf[ KEY_INPUT_ESCAPE ] == 1 ) break ;
char_disp(White,y);
Key();
if( ProcessMessage() == -1 ) break ;
ScreenFlip() ;
}
DxLib_End() ;
return 0 ;
}
void Key(void)
{
int DownOshita=0,NewDown=0,OldDown=0,DownPusingCounter=0;
GetHitKeyStateAll( KeyBuf ) ;
if( KeyBuf[ KEY_INPUT_DOWN ] == 1 )
NewDown=1;
else
NewDown=0;
if( OldDown==0 && NewDown==1 )
DownOshita=1;
else
DownOshita=0;
if( OldDown==1 && NewDown==1 )
DownPusingCounter++;
else
DownPusingCounter=0;
if( DownOshita==1 || DownPusingCounter>30)
{
if(DownPusingCounter>30)
DownPusingCounter=27;
y+=20;
if(y==260)
y=100;
}
OldDown=NewDown;
}
キー処理が早くなります。
それはローカル変数が毎回0で初期化されているからです。
Key関数が呼ばれるたびに
int DownOshita=0,NewDown=0,OldDown=0,DownPusingCounter=0;
で全部0になってしまってカウントされていません。
変数の値を保存するには、グローバル変数を使うか、staticにするかなど、しないといけません。
例えばこちらを
void Key(void)
{
int DownOshita=0,NewDown=0,OldDown=0,DownPusingCounter=0;
これを
int DownOshita=0,NewDown=0,OldDown=0,DownPusingCounter=0;
void Key(void)
{
こうしてグローバル変数にするとうまくいきますよ。
なお、グローバル変数は宣言すると0で初期化されます。
後、コードを投稿するときは<pre></pre>タグで挟んで投稿してください(<>は半角)