キーを離した後にその方向を向いた停止キャラ画像にする

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
jlok

キーを離した後にその方向を向いた停止キャラ画像にする

#1

投稿記事 by jlok » 13年前

伝わりにくいかもしれませんが…
右キーを押して右に歩かせるプログラムだけだと、右に歩くアニメーションの後、停止したときに右に歩いた画像のままとまりますよね。
それを止まったときにその方向を向いた立ち止まった画像にするのがうまくいかないのです。
GetHitKeyStateAll_2をつかって組んでみたのですが、キーを離すと数値が0になるといっても離している間はずっと0のままですよね?
それだと右に動いた後と左に動いた後が重なってしまいます…。
『キーを離した瞬間』というきっかけを組みたいのですが、検索してもなかなか出てきませんでした。
どう組めば良いのでしょうか…

このままでは重なって左に向けません…

コード:


#include "DxLib.h"


//構造体
		struct Playerdata{
			int X, Y, img;
	}Player;

 
int Key[256];
 
int GetHitKeyStateAll_2(int GetHitKeyStateAll_InputKey[]){
    char GetHitKeyStateAll_Key[256];
    GetHitKeyStateAll( GetHitKeyStateAll_Key );
    for(int i=0;i<256;i++){
        if(GetHitKeyStateAll_Key[i]==1) GetHitKeyStateAll_InputKey[i]++;
        else                            GetHitKeyStateAll_InputKey[i]=0;
    }
    return 0;
}


 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
    ChangeWindowMode(TRUE);//ウィンドウモード
    if(DxLib_Init() == -1 || SetDrawScreen( DX_SCREEN_BACK )!=0) return -1;//初期化と裏画面化

	
	// 画像をロード
	int lenga1 = LoadGraph( "image/map/lenga1.png" );
	int sea1 = LoadGraph( "image/map/sea1.png" );
	int statesbar = LoadGraph( "image/statesbar.png" ); 
	int haikei1 = LoadGraph( "image/haikei1.png" ); 
	int chara[12];
    LoadDivGraph( "image/chara.png" , 12 , 6 , 2 , 64 , 64 , chara );//画像を分割してimage配列に保存


		Player.X = 0 , Player.Y = 288 ;
	int JumpPower = 0 ;
 
    while(ProcessMessage()==0 && ClearDrawScreen()==0 && GetHitKeyStateAll_2(Key)==0 && Key[KEY_INPUT_ESCAPE]==0){
          //↑メッセージ処理          ↑画面をクリア           ↑入力状態を保存       ↑ESCが押されていない
 
 
        //ココから


			// ジャンプボタンを押していて、地面についていたらジャンプ
		if ((Key[KEY_INPUT_SPACE] >=1 ) && Player.Y == 288 ){
			JumpPower = 18 ;
		}

		//地面より高かったらジャンプの画像
		if ( Player.Y < 288 ){
			Player.img=chara[5];
		}


        if( Key[ KEY_INPUT_RIGHT ] >= 1 ) // 右キーが押されていたら
			Player.X+=3 ;	//右に動く

        if(( Key[ KEY_INPUT_RIGHT ] >= 1 ) && Player.Y == 288 ) // 右キーが押されていて地面についていたら
			Player.img=chara[(Player.X%96)/24 +1];          //右に歩くアニメ

		if(( Key[ KEY_INPUT_RIGHT ] == 0 ) && Player.Y == 288 )// 右キーが離されて地面についてたら
		   Player.img=chara[0];		//右を向く
		

		if( Key[ KEY_INPUT_LEFT  ] >= 1 ) // 左キーが押されていたら
			Player.X-=3 ;	//左に動く

		if(( Key[ KEY_INPUT_LEFT  ] >= 1 ) && Player.Y == 288 ) // 左キーが押されていて地面についていたら
			Player.img=chara[(Player.X%96)/24 +7];          //左に歩くアニメ

	// 落下処理
	Player.Y -= JumpPower ;

	// 落下加速度を加える
	JumpPower -= 1 ;


	// もし地面についていたら止まる	
	if( Player.Y > 288 )
	{
		Player.Y = 288 ;
		JumpPower = 0 ;
	}


	DrawGraph( 0, 0, haikei1, FALSE ); // 背景


	//マップ
	//レンガ
	for(int y=0 ; y<4 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+352,lenga1,FALSE);
			}
	}

	//海
	for(int y=0 ; y<1 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+388,sea1,TRUE);
			}
	}

	//ステータスバー背景
	for(int y=0 ; y<1 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+418,statesbar,FALSE);
			}
	}

	DrawGraph( Player.X, Player.Y, Player.img, TRUE ); // x,y の位置にキャラを描画



 
 
 
        ScreenFlip();
    }
 
    DxLib_End();
    return 0;
}

アバター
a5ua
記事: 199
登録日時: 13年前

Re: キーを離した後にその方向を向いた停止キャラ画像にする

#2

投稿記事 by a5ua » 13年前

キーが押されていない場合は、画像を変更しなければいいのでは?

アバター
MoNoQLoREATOR
記事: 284
登録日時: 13年前
住所: 東京

Re: キーを離した後にその方向を向いた停止キャラ画像にする

#3

投稿記事 by MoNoQLoREATOR » 13年前

キャラの向きだけを記憶しておく変数をつくります。
キーが押されていたらその方向を記憶させるようにします。(キーが押されていない時は何もしません)

こうすることで、キーが押されていても押されていなくても、キャラクタの向きを知ることができます。

jlok

Re: キーを離した後にその方向を向いた停止キャラ画像にする

#4

投稿記事 by jlok » 13年前

とりあえず、キャラの方向を入れるwalkdirectionという変数を作ってみました。
walkdirectionが左を向くと0、右を向くと1になるようにしたつもりなんですが…
左に移動した後にも右を向いて停止してしまいます。
キーを押している間しか変わらないってことはないですよね…

コード:

#include "DxLib.h"


//構造体
		struct Playerdata{
			int X, Y, img;
	}Player;

 
int Key[256];
 
int GetHitKeyStateAll_2(int GetHitKeyStateAll_InputKey[]){
    char GetHitKeyStateAll_Key[256];
    GetHitKeyStateAll( GetHitKeyStateAll_Key );
    for(int i=0;i<256;i++){
        if(GetHitKeyStateAll_Key[i]==1) GetHitKeyStateAll_InputKey[i]++;
        else                            GetHitKeyStateAll_InputKey[i]=0;
    }
    return 0;
}


 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
    ChangeWindowMode(TRUE);//ウィンドウモード
    if(DxLib_Init() == -1 || SetDrawScreen( DX_SCREEN_BACK )!=0) return -1;//初期化と裏画面化

	
	// 画像をロード
	int lenga1 = LoadGraph( "image/map/lenga1.png" );
	int sea1 = LoadGraph( "image/map/sea1.png" );
	int statesbar = LoadGraph( "image/statesbar.png" ); 
	int haikei1 = LoadGraph( "image/haikei1.png" ); 
	int chara[12];
    LoadDivGraph( "image/chara.png" , 12 , 6 , 2 , 64 , 64 , chara );//画像を分割してimage配列に保存


		Player.X = 0 , Player.Y = 288 ;
	int JumpPower = 0 ;
	int walkdirection = 1 ;

    while(ProcessMessage()==0 && ClearDrawScreen()==0 && GetHitKeyStateAll_2(Key)==0 && Key[KEY_INPUT_ESCAPE]==0){
          //↑メッセージ処理          ↑画面をクリア           ↑入力状態を保存       ↑ESCが押されていない
 
 
        //ココから


			// ジャンプボタンを押していて、地面についていたらジャンプ
		if ((Key[KEY_INPUT_SPACE] >=1 ) && Player.Y == 288 ){
			JumpPower = 18 ;
		}

		//地面より高かったらジャンプの画像
		if ( Player.Y < 288 ){
			Player.img=chara[5];
		}


        if( Key[ KEY_INPUT_RIGHT ] >= 1 ) {// 右キーが押されていたら
			Player.X+=3 ;	//右に動く
			walkdirection = 1 ;
		}

        if(( Key[ KEY_INPUT_RIGHT ] >= 1 ) && Player.Y == 288 ) // 右キーが押されていて地面についていたら
			Player.img=chara[(Player.X%96)/24 +1];          //右に歩くアニメ

		if( walkdirection = 1 && ( Key[ KEY_INPUT_RIGHT ] == 0 ) && Player.Y == 288 )// 右キーが離されて地面についてたら
		   Player.img=chara[0];		//右を向く
		

		if( Key[ KEY_INPUT_LEFT  ] >= 1 ){ // 左キーが押されていたら
			Player.X-=3 ;	//左に動く
			walkdirection = 0 ;
	}

		if(( Key[ KEY_INPUT_LEFT  ] >= 1 ) && Player.Y == 288 ) // 左キーが押されていて地面についていたら
			Player.img=chara[(Player.X%96)/24 +7];          //左に歩くアニメ

		if( walkdirection = 0 && ( Key[ KEY_INPUT_LEFT ] == 0 ) && Player.Y == 288 )// 左キーが離されて地面についてたら
		   Player.img=chara[6];		//左を向く

	// 落下処理
	Player.Y -= JumpPower ;

	// 落下加速度を加える
	JumpPower -= 1 ;


	// もし地面についていたら止まる	
	if( Player.Y > 288 )
	{
		Player.Y = 288 ;
		JumpPower = 0 ;
	}


	DrawGraph( 0, 0, haikei1, FALSE ); // 背景


	//マップ
	//レンガ
	for(int y=0 ; y<4 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+352,lenga1,FALSE);
			}
	}

	//海
	for(int y=0 ; y<1 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+388,sea1,TRUE);
			}
	}

	//ステータスバー背景
	for(int y=0 ; y<1 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+418,statesbar,FALSE);
			}
	}

	DrawGraph( Player.X, Player.Y, Player.img, TRUE ); // x,y の位置にキャラを描画



 
 
 
        ScreenFlip();
    }
 
    DxLib_End();
    return 0;
}




tokinaka

Re: キーを離した後にその方向を向いた停止キャラ画像にする

#5

投稿記事 by tokinaka » 13年前

68、80行目が比較じゃなくて代入になってますよ。
たぶんそこじゃないですか?

jlok

Re: キーを離した後にその方向を向いた停止キャラ画像にする

#6

投稿記事 by jlok » 13年前

代入を比較に直したらうまく左右に向ける様になりました!
ついでにジャンプ中も左右に向きが変えられるようにもできました。
解決しました。ありがとうございます。

一応コード貼っておきます。

コード:

#include "DxLib.h"


//構造体
		struct Playerdata{
			int X, Y, img;
	}Player;

 
int Key[256];
 
int GetHitKeyStateAll_2(int GetHitKeyStateAll_InputKey[]){
    char GetHitKeyStateAll_Key[256];
    GetHitKeyStateAll( GetHitKeyStateAll_Key );
    for(int i=0;i<256;i++){
        if(GetHitKeyStateAll_Key[i]==1) GetHitKeyStateAll_InputKey[i]++;
        else                            GetHitKeyStateAll_InputKey[i]=0;
    }
    return 0;
}


 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
    ChangeWindowMode(TRUE);//ウィンドウモード
    if(DxLib_Init() == -1 || SetDrawScreen( DX_SCREEN_BACK )!=0) return -1;//初期化と裏画面化

	
	// 画像をロード
	int lenga1 = LoadGraph( "image/map/lenga1.png" );
	int sea1 = LoadGraph( "image/map/sea1.png" );
	int statesbar = LoadGraph( "image/statesbar.png" ); 
	int haikei1 = LoadGraph( "image/haikei1.png" ); 
	int chara[12];
    LoadDivGraph( "image/chara.png" , 12 , 6 , 2 , 64 , 64 , chara );//画像を分割してimage配列に保存


		Player.X = 0 , Player.Y = 288 ;
	int JumpPower = 0 ;
	int walkdirection = 1 ;

    while(ProcessMessage()==0 && ClearDrawScreen()==0 && GetHitKeyStateAll_2(Key)==0 && Key[KEY_INPUT_ESCAPE]==0){
          //↑メッセージ処理          ↑画面をクリア           ↑入力状態を保存       ↑ESCが押されていない
 
 
        //ココから


			// ジャンプボタンを押していて、地面についていたらジャンプ
		if ((Key[KEY_INPUT_SPACE] >=1 ) && Player.Y == 288 ){
			JumpPower = 18 ;
		}

			//地面より高くて右向きなら右ジャンプの画像
		if ( walkdirection == 1 && Player.Y < 288 ){
			Player.img=chara[5];
		}

			//地面より高くて左向きなら左ジャンプの画像
		if ( walkdirection == 0 && Player.Y < 288 ){
			Player.img=chara[11];
		}


        if( Key[ KEY_INPUT_RIGHT ] >= 1 ) {// 右キーが押されていたら
			Player.X+=3 ;	//右に動く
			walkdirection = 1 ;
		}

        if(( Key[ KEY_INPUT_RIGHT ] >= 1 ) && Player.Y == 288 ) // 右キーが押されていて地面についていたら
			Player.img=chara[(Player.X%96)/24 +1];          //右に歩くアニメ

		if( walkdirection == 1 && ( Key[ KEY_INPUT_RIGHT ] == 0 ) && Player.Y == 288 )// 右キーが離されて地面についてたら
		   Player.img=chara[0];		//右を向く
		

		if( Key[ KEY_INPUT_LEFT  ] >= 1 ){ // 左キーが押されていたら
			Player.X-=3 ;	//左に動く
			walkdirection = 0 ;
	}

		if(( Key[ KEY_INPUT_LEFT  ] >= 1 ) && Player.Y == 288 ) // 左キーが押されていて地面についていたら
			Player.img=chara[(Player.X%96)/24 +7];          //左に歩くアニメ

		if( walkdirection == 0 && ( Key[ KEY_INPUT_LEFT ] == 0 ) && Player.Y == 288 )// 左キーが離されて地面についてたら
		   Player.img=chara[6];		//左を向く

	// 落下処理
	Player.Y -= JumpPower ;

	// 落下加速度を加える
	JumpPower -= 1 ;


	// もし地面についていたら止まる	
	if( Player.Y > 288 )
	{
		Player.Y = 288 ;
		JumpPower = 0 ;
	}


	DrawGraph( 0, 0, haikei1, FALSE ); // 背景


	//マップ
	//レンガ
	for(int y=0 ; y<4 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+352,lenga1,FALSE);
			}
	}

	//海
	for(int y=0 ; y<1 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+388,sea1,TRUE);
			}
	}

	//ステータスバー背景
	for(int y=0 ; y<1 ; y++){
		for(int x=0 ; x<20 ; x++){
			DrawGraph(x*32+0,y*32+418,statesbar,FALSE);
			}
	}

	DrawGraph( Player.X, Player.Y, Player.img, TRUE ); // x,y の位置にキャラを描画



 
 
 
        ScreenFlip();
    }
 
    DxLib_End();
    return 0;
}



アバター
Dixq (管理人)
管理人
記事: 1661
登録日時: 13年前
住所: 北海道札幌市
連絡を取る:

Re: キーを離した後にその方向を向いた停止キャラ画像にする

#7

投稿記事 by Dixq (管理人) » 13年前

キーを離した瞬間も検出できるように http://dixq.net/g/02_09.html を少しアレンジしてみました。
Keyがsignedなのはこういう使い方を後に紹介しようと思っていたためです。
但し、ここままだと起動直後は全てのキーが離されたと思ってしまうので注意して下さい。

コード:

#include "DxLib.h"

int Key[256]; // キーが押されているフレーム数を格納する

// キーの入力状態を更新する
int gpUpdateKey(){
	char tmpKey[256]; // 現在のキーの入力状態を格納する
	GetHitKeyStateAll( tmpKey ); // 全てのキーの入力状態を得る
	for( int i=0; i<256; i++ ){ 
		if( tmpKey[i] != 0 ){	// i番のキーコードに対応するキーが押されていたら
			if( Key[i] <= 0 ){
				Key[i] = 1;
			} else {
				Key[i]++;
			}
		} else {				// 押されていなければ
			if( Key[i] >= 0 ){
				Key[i] = -1;
			} else {
				Key[i]--;
			}
		}
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
	ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定

	// while(裏画面を表画面に反映, メッセージ処理, 画面クリア, キーの状態更新)
	while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && gpUpdateKey()==0 ){

		if( Key[KEY_INPUT_RIGHT] == 1 ){ // 右キーが押された瞬間なら
			printfDx("押された\n");
		}
		if( Key[KEY_INPUT_RIGHT] == -1 ){ // 右キーが離された瞬間なら
			printfDx("離された\n");
		}

	}

	DxLib_End(); // DXライブラリ終了処理
	return 0;
} 

閉鎖

“C言語何でも質問掲示板” へ戻る