2016-03-22 8 views
-2

私は、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;   
     } 
    } 
} 
+0

投稿したコードを読んだり見ましたか? 'class Program'はEntryPointを持つParentクラス/クラスなので、タイマーのコードを削除して子クラスの1つに配置します。他のクラスのインスタンスを作成しています。 「インスタンスクラスと静的クラス」とメソッドの間にも – MethodMan

答えて

0

あなたがいるからです最初の使用の範囲外で定義します。

まず、Characterクラスのメンバーにstaticsを使用しないでください。そうしないと、誰もが値を共有します。 第2に、knight型のオブジェクトを作成していないため、intのi = Knight.H Pを外部に宣言しないでください。

class Character 
{ 
    public int HP; 
    public int atkP = 0; 
    public bool Dash = false; 
    public double Target; //Equal to chosen enemy Loc 
    public bool isAttacking = false; 
    public int LocX = 1; 
    public int LocY = 1; 

    public Character() 
    { 
    } 

    public virtual void HandleInput() 
    { 

    } 

    //public override void Attack() 
    //{ 
    // if (isAttacking == true) 
    // { 
    //  HP -= atkP;     
    // } 
    //} 
} 

代わりに、あなたのプログラムはより多くのようにする必要があります:あなたはあなたができる代わりに、ただ一つの文字の健康を制御するためにタイマーを使用してはならない、メソッド・マンが述べたようにあなたは間違いなくあなたのプログラムを手直しする必要があり

class Program 
{ 
    private static Character Player; 

    public static void Main(string[] args) 
    { 
     Player = new Knight(); 
     timer.Elapsed += timer_Elapsed; 
     timer.Start();    
     Console.Read(); 
    } 

    private static void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     Player.HP++; 

     //Console.Clear(); 
     Console.WriteLine("================================================="); 
     Console.WriteLine(""); 
     Console.WriteLine("    Health: " + Player.HP); 
     Console.WriteLine("");   Console.WriteLine("================================================="); 

     if (i == 20) 
     { 
      timer.Stop(); 
     } 
    } 
} 

あなたのゲームの処理が必要なものを処理するように設定してください。複数のキャラクターを持っている場合は、キャラクターを個別に増やすことができますが、今のところ始まりです。

関連する問題