来自 for 循环的 Caling 类

我正在使用 Visual Studio C# 表单,我有一个具有以下属性的类:


string Name;

int NumberOfHitBoxes;

Point Hit1;

Point Hit2;

Point Hit3;

我有一个 mouseMove 事件,可以检测鼠标是否在“hitbox”中,它可以正常工作,如下所示:(在其他部分( p.Hit1.X ))


picture.MouseMove += (sender,  e) =>

                            {

                                var c = sender as PictureBox;

                                bool h=false;

                                if (null == c) return;

                                if (_dragging)

                                {

                                c.Top = e.Y + c.Top - _yPos;

                                c.Left = e.X + c.Left - _xPos;

                                }

                                else

                                {   

                                    if ((e.X>p.Hit1.X)&&(e.X<p.Hit1.X+hitw)&&(e.Y>p.Hit1.Y)&&(e.Y<p.Hit1.Y+hitw)){hitbox(c,p.Hit1.X,p.Hit1.Y,8);h=true;tb.Text = p.Type;hitok = p.Input1Point;}  

                                    if ((e.X>p.Hit2.X)&&(e.X<p.Hit2.X+hitw)&&(e.Y>p.Hit2.Y)&&(e.Y<p.Hit2.Y+hitw)){hitbox(c,p.Hit2.X,p.Hit2.Y,8);h=true;tb.Text = p.Type;hitok = p.Input2Point;} 

                                    if ((e.X>p.Hit3.X)&&(e.X<p.Hit3.X+hitw)&&(e.Y>p.Hit3.Y)&&(e.Y<p.Hit3.Y+hitw)){hitbox(c,p.Hit3.X,p.Hit3.Y,8);h=true;tb.Text = p.Type;hitok = p.Output1Point;} 

                                }

                                if (!h){picture.Refresh();h=false;hitok = new Point(0,0);}

                            };

简化:


int x = 1;

int whatever = p["Hit"+x.ToString()].X;

而不是这个:


int whatever = p.Hit1.X;

一些聪明的人也许可以教像我这样的笨蛋一些小技巧?


莫回无
浏览 34回答 1
1回答

RISEBY

你的班public class Phit{&nbsp; public List<Point> HitBoxes { get; private set; }&nbsp; public Phit(List<Point> hitboxes)&nbsp; {&nbsp; &nbsp; HitBoxes = hitboxes;&nbsp; }}使用 hitboxes 初始化您的类。List<Point> hitboxes = new List<Point>{&nbsp; &nbsp; new Point(1.2, 1.5),&nbsp; &nbsp; new Point(1.2, 3.0),&nbsp; &nbsp; new Point(1.2, 4.5)};Phit phit = new Phit(hitboxes);MouseMove 方法内的循环。foreach(Point p in phit.HitBoxes){&nbsp; &nbsp; if (e.X > p.X && e.X < p.X + hitw && e.Y > p.Y && e.Y < p.Y + hitw)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; hitbox(c, p.X, p.Y, 8);&nbsp; &nbsp; &nbsp; &nbsp; h = true;&nbsp; &nbsp; &nbsp; &nbsp; tb.Text = $"X: {p.X} / Y: {p.Y}";&nbsp; &nbsp; &nbsp; &nbsp; hitok = p;&nbsp; &nbsp; }}&nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP