c# - 如何将不同类中的图片框引用到计时器事件方法
问题描述
所以基本上,我在计算机科学高中的第一年之后尝试制作一个业余迷你 RPG 游戏,并且我正在尝试进行攻击,匕首(pictureBox2)从角色中缓慢出来,然后又回来我已经设法让匕首熄灭然后进入,但我的问题是,一旦我尝试放入不同的类,我无法将图片框传递给计时器事件,因为我的object sender
和ElapsedEventArgs e
都被渲染为“未使用” "如您所见, " sender
" 并且e
在这里没有使用,
但是当我System.Windows.Forms.PictureBox weapon
从参数中删除时,它们又回到被“使用”的状态。我的 Elapsed 事件实际上有效
那么谁能解释一下如何在另一个班级的计时器事件中使用图片框
PS我正在使用System.Timers.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));
}
推荐阅读
- javascript - 无法在字符串“文档”上创建属性“验证器”
- php - Laravel MorphTo() 导致无限加载时间和白屏
- javascript - 如何使用 Javascript 在 DOM 中访问和使用具有 ID 的动态创建的 html 元素
- c# - Polly 策略在调用 Execute() 时抛出异常
- javascript - 用 rsa 反应 js crypt 文本以在 qr 码上使用它
- reactjs - React Textfield / redux 表单组件不允许写入
- python - 如何在 pandas 数据框的多列索引上查询多个值?
- python - 在python中多行输入不同的输出
- javascript - TestCafe 在 Windows 10 笔记本电脑上不断崩溃
- php - 检查数组是否等于更大的数组