C# 如何将forms.timer传递到另一个类以同时使用?
我有一个主要的计时器,我想进入一个叫做“敌人”的类。我真的不知道如何正确地做。下面是我的代码 我的计时器在主要调用“单击”时调用“mastertmr”。点击呼叫“敌人产卵”,从那里我想能够产卵的敌人在相同的增量作为我的主定时器。我将其作为“tmr”传递 主窗口C# 如何将forms.timer传递到另一个类以同时使用?,c#,.net,forms,class,timer,C#,.net,Forms,Class,Timer,我有一个主要的计时器,我想进入一个叫做“敌人”的类。我真的不知道如何正确地做。下面是我的代码 我的计时器在主要调用“单击”时调用“mastertmr”。点击呼叫“敌人产卵”,从那里我想能够产卵的敌人在相同的增量作为我的主定时器。我将其作为“tmr”传递 主窗口 public主窗口() { 初始化组件(); 字符=新字符(背景、调度程序、键、显示器、10); 字符。位置(200100); 生物=新列表(); 敌人=新列表(); 生物。添加(角色); Display.Content=characte
public主窗口()
{
初始化组件();
字符=新字符(背景、调度程序、键、显示器、10);
字符。位置(200100);
生物=新列表();
敌人=新列表();
生物。添加(角色);
Display.Content=character.X;
随机=新随机();
enemymanager=新的enemymanager(背景、调度程序、按键、显示器,**主TMR**,10);
mastertmr=new System.Windows.Forms.Timer();
mastertmr.Interval=this.waitTime;
mastertmr.Tick+=新事件处理程序(单击);
mastertmr.Start();
}
公共无效单击(对象发送者,System.EventArgs e)
{
//灌肠经理灌肠经理;
enemymanager.spawn();
}
EnemyManager类
类EnemyManager
{
System.Windows.Forms.Timer tmr;
受保护的调度员;
受保护的画布背景;
受保护的标签显示;
受保护的Int32等待时间;
受保护的字典密钥;
公共国际一级;
公共EnemyManager(画布背景、调度程序、字典键、标签显示、System.Windows.Forms.Timer tmr、Int32 waitTime=100)
{
这个。背景=背景;
this.dispatcher=dispatcher;
this.keys=keys;
this.waitTime=waitTime;
this.tmr=tmr;
}
公共void spawn()
{
tmr.Tick+=新事件处理程序(hi);
tmr.Start();
敌人;
i++;
如果(i%100==0)
{
敌人=新敌人(背景、调度员、按键、显示器,10);
敌方位置(400,0);
}
}
public void hi(对象发送方,System.EventArgs e)
{
Display.Content=“HIIIIISDFGHDFGH”;
}
}
我不完全确定您想做什么,但我会避免将计时器传递给敌人的类。如果您试图在每个tick上同时做不止一件事,我会将“hi”方法订阅到主类上的tick事件,例如
enemymanager = new EnemyManager(Background, Dispatcher, keys, Display, 10);
mastertmr = new System.Windows.Forms.Timer();
mastertmr.Interval = this.waitTime;
mastertmr.Tick += new EventHandler(Click);
mastertmr.Tick += new EventHandler(enemymanager.hi);
mastertmr.Start();
似乎将null(tmr参数)传递给了EnemyManager构造函数?:)啊,那个么你们认为我应该通过spawn传递它吗?我想你们不需要EnemyManager类中的计时器,窗口有计时器勾号处理程序,每次勾号时你们都可以调用EnemyManager的spawn方法。这是更好的方法。是的,你是对的,我很愚蠢。谢谢。是的,这就是我想做的,我只是把事情复杂化了。非常感谢。
class EnemyManager
{
System.Windows.Forms.Timer tmr;
protected Dispatcher dispatcher;
protected Canvas Background;
protected Label Display;
protected Int32 waitTime;
protected Dictionary<String, String> keys;
public int i;
public EnemyManager(Canvas Background, Dispatcher dispatcher, Dictionary<String, String> keys, Label Display, System.Windows.Forms.Timer tmr, Int32 waitTime =100)
{
this.Background = Background;
this.dispatcher = dispatcher;
this.keys = keys;
this.waitTime = waitTime;
this.tmr = tmr;
}
public void spawn()
{
tmr.Tick += new EventHandler(hi);
tmr.Start();
Enemy enemy;
i++;
if (i % 100 == 0)
{
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(400, 0);
}
}
public void hi(object sender, System.EventArgs e)
{
Display.Content = "Hiiiiiiiiiiiiiiiiiiisdfghdfgh";
}
}
enemymanager = new EnemyManager(Background, Dispatcher, keys, Display, 10);
mastertmr = new System.Windows.Forms.Timer();
mastertmr.Interval = this.waitTime;
mastertmr.Tick += new EventHandler(Click);
mastertmr.Tick += new EventHandler(enemymanager.hi);
mastertmr.Start();