首页 > 解决方案 > 如何将不同类中的图片框引用到计时器事件方法

问题描述

所以基本上,我在计算机科学高中的第一年之后尝试制作一个业余迷你 RPG 游戏,并且我正在尝试进行攻击,匕首(pictureBox2)从角色中缓慢出来,然后又回来我已经设法让匕首熄灭然后进入,但我的问题是,一旦我尝试放入不同的类,我无法将图片框传递给计时器事件,因为我的object senderElapsedEventArgs e 都被渲染为“未使用” "如您所见, " sender" 并且e在这里没有使用, 但是当我System.Windows.Forms.PictureBox weapon从参数中删除时,它们又回到被“使用”的状态。我的 Elapsed 事件实际上有效

那么谁能解释一下如何在另一个班级的计时器事件中使用图片框

PS我正在使用System.Timers.Timer,第三张照片应该显示我如何设置计时器我的计时器设置方法

标签: c#timer

解决方案


保留 Elapsed() 事件的签名,因为它应该在参数中仅包含“sender,e”。

将连接事件的线路更改为:

timer1.Elapsed += Timer1_Elapsed;

然后将“sender”投射到 Timer,并通过将“SynchronizingObject”投射到 PictureBox 来追逐它:

private static void Timer1_Elapsed(object sender, ElapsedEventArgs e)
{
    System.Timers.Timer timer = (System.Timers.Timer)sender;
    PictureBox weapon = (PictureBox)timer.SynchronizingObject;
    AttackMov(weapon));
}

推荐阅读