私は、HPの損傷後に治癒のための基本的なタイマー機能を設定しようとしています。タイマーは動作していますが、1文字に割り当てられた子クラスの代わりに親Characterクラスから呼び出されているため、その文字に残っているHPの数ではなく0から数えています。親の代わりに子供にアクセスするにはどうすればいいですか?メインプログラムの子クラスからの情報の取得
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace hpTimer_Test
{
class Program
{
static Timer timer = new Timer(3000);
static int i = Knight.HP;
static void Main(string[] args)
{
timer.Elapsed += timer_Elapsed;
timer.Start();
Console.Read();
}
private static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
i++;
//Console.Clear();
Console.WriteLine("=================================================");
Console.WriteLine("");
Console.WriteLine(" Health: " + i.ToString());
Console.WriteLine("");
Console.WriteLine("=================================================");
if (i == 20)
{
timer.Stop();
}
GC.Collect();
}
}
}
----------文字クラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hpTimer_Test
{
class Character
{
public static int HP = 0;
public static int atkP = 0;
public static bool Dash = false;
public static double Target; //Equal to chosen enemy Loc
public static bool isAttacking = false;
public static int LocX = 1;
public static int LocY = 1;
public Character()
{
}
public void HandleInput()
{
}
//public override void Attack()
//{
// if (isAttacking == true)
// {
// HP -= atkP;
// }
//}
}
}
---------- KNIGHT CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hpTimer_Test
{
class Knight : Character
{
public Knight()
{
HP = 10;
atkP = 10;
Dash = true;
isAttacking = false;
}
}
}
投稿したコードを読んだり見ましたか? 'class Program'はEntryPointを持つParentクラス/クラスなので、タイマーのコードを削除して子クラスの1つに配置します。他のクラスのインスタンスを作成しています。 「インスタンスクラスと静的クラス」とメソッドの間にも – MethodMan