クラスの作成にあたって

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

クラスの作成にあたって

#1

投稿記事 by pudding » 14年前

今、C++を勉強しながらゲームを作っています。
ブロックゲームのキーを押したら移動するという簡単な処理を書いているのですが、
いきなり詰まってしまって何かアドバイスを頂けたらと思います。

キーに関するクラス

コード:

#ifndef _CKEY_H_
#define _CKEY_H_

class CKey {
public:
	CKey();
	int GetHitKeyStateAll_2();
	int GetInputKey(int KeyCode);
	
private:
	int InputKey[256];
};

#endif

コード:

#include "DxLib.h"
#include "CKey.h"

// コンストラクタ
CKey::CKey() {
	return ;
}

// キーが押されているか判断する
int CKey::GetHitKeyStateAll_2() {
	char GetHitKeyStateAll_Key[256];
	GetHitKeyStateAll(GetHitKeyStateAll_Key);
	for (int i = 0; i < 256; i++) {
		if (GetHitKeyStateAll_Key[i] == 1)	this->InputKey[i]++;
		else								this->InputKey[i] = 0;
	}
	return 0;
}

// 指定されたキーコードの入力状態を返す
int CKey::GetInputKey(int KeyCode) {
	return this->InputKey[KeyCode];
}
移動に関するクラス

コード:

#ifndef _CBLOCK_H_
#define _CBLOCK_H_

class CBlock {
public:
	CBlock();
	void Block_Move();
}

#endif

コード:

#include "DxLib.h"
#include "CBlock.h"
#include "CKey.h"

CBlock::CBlock() {
	return ;
}

void CBlock::Block_Move() {
	if (CKey::GetInputKey(KEY_INPUT_UP) != 0)
	return ;
}
このような場合だとCBlock::Block_Moveでエラーが出てしまい
CBlock::Block_Move関数内の
上キーを押された状態を取得する場合にはどのようにコードを記述するのが良い方法なのでしょうか

開発環境はVC++ 2010です
よろしくお願いします。

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 15年前
住所: 東海地方
連絡を取る:

Re: クラスの作成にあたって

#2

投稿記事 by softya(ソフト屋) » 14年前

こんな風にしてクラスのインスタンスを作るとか
無理やり一つにまとめてあります。

コード:

#include "DxLib.h"

#ifndef _CKEY_H_
#define _CKEY_H_
 
class CKey {
public:
    CKey();
    int GetHitKeyStateAll_2();
	int GetInputKey(int KeyCode);
    
private:
	int InputKey[256];
};
 
#endif

// コンストラクタ
CKey::CKey() {
    return ;
}
 
// キーが押されているか判断する
int CKey::GetHitKeyStateAll_2() {
    char GetHitKeyStateAll_Key[256];
    GetHitKeyStateAll(GetHitKeyStateAll_Key);
    for (int i = 0; i < 256; i++) {
        if (GetHitKeyStateAll_Key[i] == 1)  this->InputKey[i]++;
        else                                this->InputKey[i] = 0;
    }
    return 0;
}
 
// 指定されたキーコードの入力状態を返す
int CKey::GetInputKey(int KeyCode) {
    return InputKey[KeyCode];
}

#ifndef _CBLOCK_H_
#define _CBLOCK_H_
 
class CBlock {
public:
    CBlock();
    void Block_Move();
private:
	CKey ckey;
};
 
#endif

CBlock::CBlock() {
    return ;
}
 
void CBlock::Block_Move() {
    if (ckey.GetInputKey(KEY_INPUT_UP) != 0)
    return ;
}
あるいは、完全にstatic化してしまうとかどうでしょう?

コード:

#include "DxLib.h"
 

#ifndef _CKEY_H_
#define _CKEY_H_
 
class CKey {
private:
    CKey();
	static int InputKey[256];
public:
    static int GetHitKeyStateAll_2();
	static int GetInputKey(int KeyCode);
};
 
#endif

//	実体
int CKey::InputKey[256];

// コンストラクタ
CKey::CKey() {
    return ;
}

// キーが押されているか判断する
int CKey::GetHitKeyStateAll_2() {
    char GetHitKeyStateAll_Key[256];
    GetHitKeyStateAll(GetHitKeyStateAll_Key);
    for (int i = 0; i < 256; i++) {
        if (GetHitKeyStateAll_Key[i] == 1)  CKey::InputKey[i]++;
        else                                CKey::InputKey[i] = 0;
    }
    return 0;
}
 
// 指定されたキーコードの入力状態を返す
int CKey::GetInputKey(int KeyCode) {
    return CKey::InputKey[KeyCode];
}

#ifndef _CBLOCK_H_
#define _CBLOCK_H_
 
class CBlock {
public:
    CBlock();
    void Block_Move();
};
 
#endif

CBlock::CBlock() {
    return ;
}
 
void CBlock::Block_Move() {
    if (CKey::GetInputKey(KEY_INPUT_UP) != 0)
    return ;
}
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

pudding

Re: クラスの作成にあたって

#3

投稿記事 by pudding » 14年前

お答えありがとうございます。

インスタンスのほうを使ってコードを書き直してみたところうまく出来ました。
また、よろしくお願いします。

閉鎖

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