自分なりにコードをかいたのですが、どうもうまくいかないようです。
先に困ったところ、聞きたいところをあげておきます。
1.弾生成の手順および弾生成メソッドの場所。ISLeさんに、自機クラスにはいれないと言われましたが、解決策を理解しきれておらず、どうしようか困って結局自機クラスに入れました。CMyShip::Shot(CMyBulletManage)がそれですが、このように引数に他クラスを渡すしか異クラス間のデータのやり取りの方法が分からなかったためこうなりました。
2.上記の影響で、分割コンパイルすると、クラスが未宣言扱いだったり、多重定義だったりしてしまい、デバッグ以前の問題になってしまいます。上手なコンパイル方法が分からないです。#ifndef#endifを使うと思うのですが、どこにどうするべきかが分からないです。(MyShip.hにMyBullet.hをインクルードすると多重定義、インクルードしないとクラス未定義で怒られます。)
今回もご迷惑をおかけしますが、よろしくお願いします。
main.cpp
#include "Game.h"
#include"DxLib.h"
// main関数の開始
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode(TRUE) ; // debug用ウィンドウサイズ
if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
{
return -1 ; // エラーが起きたら直ちに終了
}
SetDrawScreen( DX_SCREEN_BACK ) ; // 描画先画面を裏画面にする
CGame Game;
while (1) {
ClearDrawScreen() ; // 画面クリア
// 本文の記述
Game.Process();
ScreenFlip() ; // redraw 1
WaitTimer(17) ; // 約60fps
if (ProcessMessage() == -1 ) break ; // windowsメッセージ処理
if ( CheckHitKey(KEY_INPUT_ESCAPE ) == 1) break ; // escキーで終了
}
DxLib_End() ;//DXライブラリ終了処理
return 0 ;
}
#include"MyBullet.h"
#include"MyShip.h"
class CGame{
private:
CMyShip MyShip;
CMyBulletManage MyBulletManage;
public:
void Process();
};
class CMyShip{
private:
int x,y,v;
int GraphHandle[4];
int handlec,c;
public:
CMyShip();
void Move();
void Draw();
void Shot(CMyBulletManage MyBulletManage);
void Process(CMyBulletManage MyBulletManage);
};
class CMyBullet{
private:
int x,y;
static const int v;
bool f;
int GraphHandle[8];
public:
CMyBullet();
void Set(int x,int y);
void Move();
void Draw();
bool IsAllocated();
};
class CMyBulletManage{
private:
CMyBullet MyBullet[30];
int WaitKeyc;
public:
CMyBulletManage();
void Set(int x,int y);
void Process();
};
#include"Game.h"
void CGame::Process(){
MyShip.Process(MyBulletManage);
MyBulletManage.Process();
}
#include"DxLib.h"
#include"MyShip.h"
CMyShip::CMyShip(){
CMyShip::x=320;
CMyShip::y=400;
CMyShip::v=3;
CMyShip::handlec=0;
CMyShip::c=0;
LoadDivGraph("jiki.png",4,4,1,24,32,CMyShip::GraphHandle);
}
void CMyShip::Move(){
if(CheckHitKey(KEY_INPUT_LEFT)==1){
CMyShip::x-=CMyShip::v;
if(CMyShip::x<0) CMyShip::x=0;
}
if(CheckHitKey(KEY_INPUT_UP)==1){
CMyShip::y-=CMyShip::v;
if(CMyShip::y<0) CMyShip::y=0;
}
if(CheckHitKey(KEY_INPUT_RIGHT)==1){
CMyShip::x+=CMyShip::v;
if(CMyShip::x>640-24) CMyShip::x=616;
}
if(CheckHitKey(KEY_INPUT_DOWN)==1){
CMyShip::y+=CMyShip::v;
if(CMyShip::y>454) CMyShip::y=454;
}
}
void CMyShip::Draw(){
if(CMyShip::c%16==0) CMyShip::handlec++;
if(CMyShip::handlec==4) CMyShip::handlec=0;
DrawGraph(CMyShip::x,CMyShip::y,CMyShip::GraphHandle[CMyShip::handlec],TRUE);
}
void CMyShip::Shot(CMyBulletManage MyBulletManage){
MyBulletManage.Set(CMyShip::x,CMyShip::y);
}
void CMyShip::Process(CMyBulletManage MyBulletManage){
CMyShip::Move();
CMyShip::Shot(MyBulletManage);
CMyShip::Draw();
}
#include"DxLib.h"
#include"MyBullet.h"
const int CMyBullet::v=8;
CMyBullet::CMyBullet(){
CMyBullet::x=0;
CMyBullet::y=0;
CMyBullet::f=FALSE;
LoadDivGraph("jtm.png",8,8,1,16,18,CMyBullet::GraphHandle);
}
void CMyBullet::Set(int x,int y){
CMyBullet::x=x;
CMyBullet::y=y;
CMyBullet::f=TRUE;
}
void CMyBullet::Move(){
CMyBullet::y-=CMyBullet::v;
if(CMyBullet::y<0) CMyBullet::f=FALSE;
}
void CMyBullet::Draw(){
DrawGraph(CMyBullet::x,CMyBullet::y,CMyBullet::GraphHandle[0],TRUE);
}
bool CMyBullet::IsAllocated(){
return CMyBullet::f;
}
CMyBulletManage::CMyBulletManage(){
CMyBulletManage::WaitKeyc=0;
}
void CMyBulletManage::Set(int x,int y){
int i;
if(CheckHitKey(KEY_INPUT_SPACE)==1){
CMyBulletManage::WaitKeyc++;
if(CMyBulletManage::WaitKeyc%5==1){
for(i=0;i<30;i++){
if(MyBullet[i].IsAllocated()==TRUE) continue;
MyBullet[i].Set(x,y);
break;
}
}
}
else CMyBulletManage::WaitKeyc=0;
}
void CMyBulletManage::Process(){
int i;
for(i=0;i<30;i++){
if(MyBullet[i].IsAllocated()==FALSE) continue;
MyBullet[i].Move();
MyBullet[i].Draw();
}
}