ページ 11

オプションを選択したいのですが…

Posted: 2015年7月22日(水) 12:30
by kishika
スタート画面でaを押せばAの装備に、bを押せばBの装備に
なるようにしたいのですが
main.cppに以下のようにして選択画面を作ってその後
cshot.cppで使い分けて使おうとしたところ
ショットが打てないという事態に陥ってしまいました。
初心者なんでがばがばだと思うので、
訂正や関数の使い方など教えていただけると嬉しいです。

main.cpp

コード:

void Ch_shootkind(int s){
		if(CheckStateKey(KEY_INPUT_A)==1){
			s = 0;
			func_state=99;
		}
		if(CheckStateKey(KEY_INPUT_B)==1){
			s = 1;
			func_state=99;
		}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
			case 90://start 
				Color = GetColor( 255 , 255 , 255 ) ;
				DrawFormatString(215,175,Color,"you can start the game.");
				DrawFormatString(255,195,Color,"option A -> a");
				DrawFormatString(255,215,Color,"option B -> b");
				Ch_shootkind();
				
				break;
//////////////////////////////////////////////////////////////////////////////////////////////////
cshot.cpp

コード:

void enter_shot(){
	if(Ch_shootkind = 0){
	//ショットボタンが押されていたら
	if(CheckStatePad(configpad.shot)>0){
		ch.shot_cnt++;
		if(ch.shot_cnt%3==0){//3カウントに1回
			if(CheckStatePad(configpad.slow)>0)//低速移動中なら
				ch0_shot1_pattern();
			else
				ch0_shot0_pattern();
		}
	else
		ch.shot_cnt=0;
	}
	}else  if(Ch_shootkind = 1){
		if(CheckStatePad(configpad.shot)>0){
	        	ch.shot_cnt++;
	         	if(ch.shot_cnt%3==0){//3カウントに1回
	          		if(CheckStatePad(configpad.slow)>0)//低速移動中なら
	         			ch1_shot1_pattern();
	         		else
		         		ch1_shot0_pattern();
	        	}
           	else
	        ch.shot_cnt=0;
	        }

        }
}

Re: オプションを選択したいのですが…

Posted: 2015年7月22日(水) 12:45
by みけCAT
提示されているcshot.cppの2行目と15行目が不自然ですが、これは直接の原因ではないですね。
kishika さんが書きました:

コード:

		if(CheckStatePad(configpad.shot)>0){
	        	ch.shot_cnt++;
	         	if(ch.shot_cnt%3==0){//3カウントに1回
	          		if(CheckStatePad(configpad.slow)>0)//低速移動中なら
	         			ch1_shot1_pattern();
	         		else
		         		ch1_shot0_pattern();
	        	}
           	else
	        ch.shot_cnt=0;
	        }

        }
ch.shot_cntがint型で、初期値が0だと仮定します。
今、CheckStatePad(configpad.shot)>0が真だとします。
すると、ch.shot_cnt++;が実行され、ch.shot_cntが1になります。
その次のch.shot_cnt%3==0は偽なので、else以降のch.shot_cnt=0;が実行され、ch.shot_cntは0に戻ります。
よって、ch1_shot1_pattern()やch1_shot0_pattern()は呼び出されず、これらの関数でのみショットを打っている場合は、ショットが打たれません。
インデントを整えることをおすすめします。
また、Ch_shootkindがint型だと仮定するとCh_shootkind = 0は常に偽なので、enter_shot関数の前半は実行されません。

Re: オプションを選択したいのですが…

Posted: 2015年7月23日(木) 12:41
by kishika
main.cppでAを選択したら0を返す…みたいな感じにしたくCh_shootkind(int s)を作ったのですが、
他で宣言したのを(今回はmain.cpp)を呼び出すにはexternというのを使うと見つけたので

コード:

extern void Ch_shootkind(int s)
とやってみましたがやっぱり打たれませんでした。
そして回答にあったことを意識して以下のように書き換えてみました。
main.cpp

コード:

void Ch_shootkind(){
	int s;
	if(CheckStateKey(KEY_INPUT_A)==1){
		s = 0;
		func_state=99;
	}
	if(CheckStateKey(KEY_INPUT_B)==1){
		s = 1;
		func_state=99;
	}
}
///////////////////////////////////////////////////////////

			case 90://start 
				Color = GetColor( 255 , 255 , 255 ) ;
				DrawFormatString(215,175,Color,"you can stert the game.");
				DrawFormatString(255,195,Color,"option A -> a");
				DrawFormatString(255,215,Color,"option B -> b");
				Ch_shootkind();
				
				break;

cshot.cpp

コード:

extern int Ch_shootkind;
///////////////////////////////////////////////////
void enter_shot0(){
	//ショットボタンが押されていたら
	if(CheckStatePad(configpad.shot)>0){
		ch.shot_cnt++;
		if(ch.shot_cnt%3==0){//3カウントに1回
			if(CheckStatePad(configpad.slow)>0)//低速移動中なら
				ch0_shot1_pattern();
			else
				ch0_shot0_pattern();
		}
	else
		ch.shot_cnt=0;
	}
}
void enter_shot1(){
		if(CheckStatePad(configpad.shot)>0){
		ch.shot_cnt++;
		if(ch.shot_cnt%3==0){//3カウントに1回
			if(CheckStatePad(configpad.slow)>0)//低速移動中なら
				ch1_shot1_pattern();
			else
				ch1_shot0_pattern();
		}
	else
		ch.shot_cnt=0;
	}
}
//////////////////////////////////////////////////////////////////////////////

void cshot_main(){
	calc_option_bb();//オプションのぼんぼん計算(49)
	calc_cshot();//ショットの起動計算
	if (Ch_shootkind =0){
	enter_shot0();//ショット登録
	}else if (Ch_shootkind =1){
	enter_shot1();//ショット登録
	}
}
としてみました。
cshot_main内のCh_shootkindがエラーをはいてるんですがよくわかんなかったです。

Re: オプションを選択したいのですが…

Posted: 2015年7月23日(木) 13:28
by みけCAT
kishika さんが書きました:main.cppでAを選択したら0を返す…みたいな感じにしたくCh_shootkind(int s)を作ったのですが、
他で宣言したのを(今回はmain.cpp)を呼び出すにはexternというのを使うと見つけたので

コード:

extern void Ch_shootkind(int s)
とやってみましたがやっぱり打たれませんでした。
この表現は今は書かれていないですよね?
もし書かれていたら消すべきかもしれません。
内容や位置がわかりませんが、衝突しそうなので作ったCh_shootkind(int s)も消すべきかもしれません。
> 他で宣言したのを(今回はmain.cpp)を呼び出すにはexternというのを使うと見つけたので
他の翻訳単位で定義した変数を使う方法の一つとしてexternが使えますが、関数の呼び出しには使わなくていいです。
kishika さんが書きました:そして回答にあったことを意識して以下のように書き換えてみました。
指摘したこと(不適切な条件式、不適切な0代入、インデント)が何も改善されていないですね。
kishika さんが書きました:cshot_main内のCh_shootkindがエラーをはいてるんですがよくわかんなかったです。
いい設計かは微妙ですが、とりあえずこうしてみてください。
テストはしていません。

main.cpp

コード:

int Ch_shootkind;
void Ch_shootkainndo(){
	if(CheckStateKey(KEY_INPUT_A)==1){
		Ch_shootkind = 0;
		func_state=99;
	}
	if(CheckStateKey(KEY_INPUT_B)==1){
		Ch_shootkind = 1;
		func_state=99;
	}
}
///////////////////////////////////////////////////////////

			case 90://start 
				Color = GetColor( 255 , 255 , 255 ) ;
				DrawFormatString(215,175,Color,"you can stert the game.");
				DrawFormatString(255,195,Color,"option A -> a");
				DrawFormatString(255,215,Color,"option B -> b");
				Ch_shootkainndo();
				
				break;

cshot.cpp

コード:

extern int Ch_shootkind;
///////////////////////////////////////////////////
void enter_shot0(){
	//ショットボタンが押されていたら
	if(CheckStatePad(configpad.shot)>0){
		ch.shot_cnt++;
		if(ch.shot_cnt%3==0){//3カウントに1回
			if(CheckStatePad(configpad.slow)>0)//低速移動中なら
				ch0_shot1_pattern();
			else
				ch0_shot0_pattern();
		}
	}
	else
		ch.shot_cnt=0;
}
void enter_shot1(){
	if(CheckStatePad(configpad.shot)>0){
		ch.shot_cnt++;
		if(ch.shot_cnt%3==0){//3カウントに1回
			if(CheckStatePad(configpad.slow)>0)//低速移動中なら
				ch1_shot1_pattern();
			else
				ch1_shot0_pattern();
		}
	}
	else
		ch.shot_cnt=0;
}
//////////////////////////////////////////////////////////////////////////////

void cshot_main(){
	calc_option_bb();//オプションのぼんぼん計算(49)
	calc_cshot();//ショットの起動計算
	if (Ch_shootkind ==0){
		enter_shot0();//ショット登録
	}else if (Ch_shootkind ==1){
		enter_shot1();//ショット登録
	}
}