gpt4 book ai didi

c# - 寻求有关让事件正常运作的建议

转载 作者:行者123 更新时间:2023-12-02 04:42:04 25 4
gpt4 key购买 nike

我正在寻求一些帮助来理解事件。我一直在阅读有关它们的文章并观看教程视频。

我几乎理解它们,但我总是遇到障碍。

我为自己制作了一个简单的 WinForms 测试应用程序来尝试学习这个过程。在应用程序中,有 2 个步行 Sprite 在屏幕上奔跑。当您单击表单时,它会创建一个下落的 thwomp Sprite (并创建一个事件),步行者 Sprite 应该通过选择一条远离 Sprite 的新步行路径来对事件作出 react 。我认为我已经正确地编写了所有内容,但是当我编译它时出现错误:

Error 1 Inconsistent accessibility: parameter type 'eventStomper.RunEventArgs' is less accessible than delegate 'eventStomper.RunInFear'
Error 2 Inconsistent accessibility: parameter type 'eventStomper.RunEventArgs' is less accessible than method 'eventStomper.Walker.RunAway(object, eventStomper.RunEventArgs)'

我很茫然,因为一切都是公开的。关于那里的错误有什么建议吗?而且,关于事件处理的任何建议?

这是归结为相关部分的源代码:

namespace eventStomper
{

public delegate void RunInFear(object sender, RunEventArgs re); //The delegate for responding to events.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
spawnWalkers(); //Create a couple of walkers to roam around the form

}
List<Thwomp> thowmpList = new List<Thwomp>(); //List of thwomps. This is iterated through for animation.
List<Walker> walkerList = new List<Walker>();// Same thing with the walkers.

public void pictureBox1_Click(object sender, EventArgs e) //When you click on the background, it spawns a thwomp
{
Point _spawnPoint = this.PointToClient(Cursor.Position);


Thwomp _thwomp = new Thwomp(_spawnPoint, sprite ); //Generate a new Thwomp
thowmpList.Add(_thwomp); //Add it to the list of Thwomps
_thwomp.TimeToRun += walkerList[0].RunAway; //Register with the two walkers roaming around.
_thwomp.TimeToRun += walkerList[1].RunAway;

//Do other things to setup the thwomp sprite

}


}

public class Thwomp
{
public int spriteX = 0;//Current sprite location
public int spriteY = 0;
public int targetX = 0;//Where the thwomp will land.
public int targetY = 0;


public event RunInFear TimeToRun;

public void Animate()
{
//Do Animation steps.
}
public Thwomp(Point spawnPoint, PictureBox spriteIncoming)
{
RunEventArgs re = new RunEventArgs();
re._pointOfFear = spawnPoint;

//Setup thwomp sprite
TimeToRun(this, re); //Trigger the event.
}
}

public class Walker
{
public int spriteX = 0; //Current sprite location
public int spriteY = 0;

public Walker(Point spawnPoint, PictureBox spriteIncoming)
{
//Create the walker
}

public void RunAway(Point dangerPoint)
{
if (Math.Abs(sprite.Top - dangerPoint.Y) < 20 && Math.Abs(sprite.Left - dangerPoint.X) < 20) //If near a newly created thwomp, run away.
{
//Pick a path headed away from the danger.
}
}

public void Animate()
{
//Move the walker away.
}
}

class RunEventArgs : EventArgs
{
public Point _pointOfFear;
}
}

最佳答案

I'm at a loss because everything is public.

不完全是。正如错误消息所说:

parameter type 'eventStomper.RunEventArgs' is less accessible than delegate 'eventStomper.RunInFear'

根据该消息,RunEventArgsRunInFear 更难访问。因此,让我们看看这两种类型的可访问性级别:

public delegate void RunInFear(object sender, RunEventArgs re);

所以,这是公开的。到目前为止,还不错。

class RunEventArgs : EventArgs 
{
public Point _pointOfFear;
}

啊哈!这个没有分配给可访问性,这意味着 - 根据 docs - 它将默认为 internal:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

因此,将 RunEventArgs 类设置为 public 并且您的代码应该可以编译。

关于c# - 寻求有关让事件正常运作的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com