クラス オブジェクトの 1 つのインスタンスを作成して、同じ形式の複数のプライベート関数で使用したいと考えています。私が現在持っているのは、次のように新しいオブジェクトを作成するすべての関数です。
Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(DexBox.Text), int.Parse(IntBox.Text), int.Parse(PerBox.Text), int.Parse(HPBox.Text), int.Parse(SPBox.Text), int.Parse(MPBox.Text), int.Parse(EXPBox.Text), int.Parse(ARBox.Text), int.Parse(CTHBox.Text), int.Parse(GoldBox.Text), int.Parse(MeleeDMGBox.Text), int.Parse(MagicDMGBox.Text), int.Parse(StealthBox.Text), int.Parse(DetectBox.Text), int.Parse(LevelBox.Text));
これを 4 つの異なる関数に入れなければなりません。多くの変数を持つ巨大なオブジェクトであることがわかります。新しい変数を作成すると、関数全体にこの獣をコピーして貼り付ける必要があるため、すべてのオブジェクトで使用するために一度作成できるようにしたいと考えています。
どうやってやるの?私はOOPが初めてなので、うまくいくかどうかわからないので、これが私がやろうとしたことです:
public void PlayerInitialize()
{
string P = "p";
Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(DexBox.Text), int.Parse(IntBox.Text), int.Parse(PerBox.Text), int.Parse(HPBox.Text), int.Parse(SPBox.Text), int.Parse(MPBox.Text), int.Parse(EXPBox.Text), int.Parse(ARBox.Text), int.Parse(CTHBox.Text), int.Parse(GoldBox.Text), int.Parse(MeleeDMGBox.Text), int.Parse(MagicDMGBox.Text), int.Parse(StealthBox.Text), int.Parse(DetectBox.Text), int.Parse(LevelBox.Text), P);
}
private void AddStrButton_Click_1(object sender, EventArgs e)
{
Player thePlayer;
//int Str, int Dex, int Intel, int Per, int HP, int SP, int MP, int EXP, int AR, int CTH, int Gold, int MelDMG, int MagDMG, int Stlth, int Det
//Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(DexBox.Text), int.Parse(IntBox.Text), int.Parse(PerBox.Text), int.Parse(HPBox.Text), int.Parse(SPBox.Text), int.Parse(MPBox.Text), int.Parse(EXPBox.Text), int.Parse(ARBox.Text), int.Parse(CTHBox.Text), int.Parse(GoldBox.Text), int.Parse(MeleeDMGBox.Text), int.Parse(MagicDMGBox.Text), int.Parse(StealthBox.Text), int.Parse(DetectBox.Text), int.Parse(LevelBox.Text), );
if (thePlayer.StatPoints >= 1 && thePlayer.Strength <= 7)
{
thePlayer.Strength++;
thePlayer.StatPoints--;
thePlayer.HitPoints += 6;
thePlayer.MeleeDMG += 2;
StrBox.Text = thePlayer.Strength.ToString();
SPBox.Text = thePlayer.StatPoints.ToString();
HPBox.Text = thePlayer.HitPoints.ToString();
MeleeDMGBox.Text = thePlayer.MeleeDMG.ToString();
}
else if (thePlayer.Strength >= 8)
{
MessageBox.Show("You are at the limits of your mortal abilities.");
}
else if (thePlayer.StatPoints <= 0)
{
MessageBox.Show("Earn more experience!");
}
/*
MessageBox.Show("PCStats.StatPoints equals" + PCStats.StatPoints);
MessageBox.Show("PCStats,Strength equals" + PCStats.Strength);
MessageBox.Show("IntPCSP Equals" + IntPCSP.ToString());
MessageBox.Show("IntPCStr Equals" + IntPCStr.ToString());
*/
}
残念ながら、次のエラーが表示されます: 割り当てられていないローカル変数 'thePlayer' の使用
オブジェクトを関数で使用するたびに作成するのではなく、オブジェクトを一度作成する方法についてのアイデアはありますか?
さらに参照するためのクラスシートは次のとおりです。
namespace PlayerPC
{
class Player
{
public void PlayerGraphic(string pcGraphic)
{
PCGraphic = pcGraphic;
}
//player stats
public Player(int Str, int Dex, int Intel, int Per, int HP, int SP, int MP, int EXP, int AR, int CTH, int Gold, int MelDMG, int MagDMG, int Stlth, int Det, int Lvl, string pcGraphic)
{
Strength = Str;
Dexterity = Dex;
Intelligence = Intel;
Perception = Per;
HitPoints = HP;
StatPoints = SP;
MindPoints = MP;
ExperiencePoints = EXP;
Armor = AR;
ChanceToHit = CTH;
GoldCoins = Gold;
MeleeDMG = MelDMG;
MagicDMG = MagDMG;
Stealth = Stlth;
Detect = Det;
Level = Lvl;
PCGraphic = pcGraphic;
}
public bool LevelUp()
{
if(this.Level == 1 && this.ExperiencePoints >= 25)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 2 && this.ExperiencePoints >= 125)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 3 && this.ExperiencePoints >= 625)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 4 && this.ExperiencePoints >= 1125)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 5 && this.ExperiencePoints >= 1850)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 6 && this.ExperiencePoints >= 2900)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 7 && this.ExperiencePoints >= 4500)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 8 && this.ExperiencePoints >= 5800)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 9 && this.ExperiencePoints >= 8200)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 10 && this.ExperiencePoints >= 12800)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 11 && this.ExperiencePoints >= 16200)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
else if (this.Level == 12 && this.ExperiencePoints >= 25000)
{
this.Level++;
this.StatPoints++;
MessageBox.Show("You have accended to Level " + this.Level);
return true;
}
return false;
}
public int Strength {get; set; }
public int Dexterity { get; set; }
public int Intelligence { get; set; }
public int Perception { get; set; }
public int HitPoints { get; set; }
public int StatPoints { get; set; }
public int MindPoints { get; set; }
public int ExperiencePoints { get; set; }
public int Armor { get; set; }
public int ChanceToHit { get; set; }
public int GoldCoins { get; set; }
public int MeleeDMG { get; set; }
public int MagicDMG { get; set; }
public int Stealth { get; set; }
public int Detect { get; set; }
public int Level { get; set; }
public string PCGraphic { get; set; }
}
}