.NET 4.x Не получается победить Timer - C#
Формулировка задачи:
Заставил эллипс бегать к точке клика мыши, но вот остановить его движение - никак не получается.
Все варианты, типа "Timer.Stop()" и "Timer.Enable = false" перепробовал.. эллипс не поддаётся.
Доходя до нужной точки он трясётся как руки алкаша. Как заставить его замереть?
Листинг программы
- using System;
- namespace MouseMove
- {
- public partial class Form1 : Form
- {
- double _x; //координаты эллипса
- double _y; //координаты эллипса
- double xM; //координаты мыши
- double yM; //координаты мыши
- double angle; //угол поворота
- double speed; //скорость движения
- public Form1()
- {
- InitializeComponent();
- _x = 50;
- _y = 50;
- angle = 0;
- speed = 30;
- }
- //Ищем угол между координатами сферы и курсора мыши
- double GetAngle()
- {
- return Math.Atan2((_x - xM), (_y - yM));
- }
- public void Form1_Paint(object sender, PaintEventArgs e)
- {
- e.Graphics.FillEllipse(Brushes.Black, (int)_x, (int)_y, 100, 100); //рисуем эллипс
- e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //сглаживаем движение
- }
- //Таймер
- public void tmrMoving_Tick(object sender, EventArgs e)
- {
- _x -= speed * Math.Sin(angle);
- _y -= speed * Math.Cos(angle);
- angle = GetAngle();
- Invalidate();
- }
- //Событие Клик Мыши
- public void Form1_MouseClick(object sender, MouseEventArgs e)
- {
- tmrMoving.Enabled = true;
- xM = e.X;
- yM = e.Y;
- // Проверка не получается
- // if (GetAngle() == 0) { tmrMoving.Enabled = false; }
- }
- }
- }
Решение задачи: «.NET 4.x Не получается победить Timer»
textual
Листинг программы
- namespace WindowsFormsApplication
- {
- partial class MainForm
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- this.RenderingTimer = new System.Windows.Forms.Timer(this.components);
- this.SuspendLayout();
- //
- // RenderingTimer
- //
- this.RenderingTimer.Interval = 200;
- this.RenderingTimer.Tick += new System.EventHandler(this.RenderingTimer_Tick);
- //
- // MainForm
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(284, 262);
- this.DoubleBuffered = true;
- this.Name = "MainForm";
- this.Text = "Form";
- this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
- this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseClick);
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.Timer RenderingTimer;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д