Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 寻求关于让活动发挥作用的建议_C#_Events - Fatal编程技术网

C# 寻求关于让活动发挥作用的建议

C# 寻求关于让活动发挥作用的建议,c#,events,C#,Events,我正在寻找一些帮助来了解事件。我一直在读关于它们的文章,看教程视频 我几乎能理解他们,但我总是遇到困难 我为自己制作了一个简单的WinForms测试应用程序来尝试和学习这个过程。在这个应用程序中,有两个行走的精灵在屏幕上奔跑。 当你点击表单时,它会创建一个坠落的小精灵(并创建一个事件)。步行者小精灵应该通过选择一条远离小精灵的新行走路径来对事件做出反应。我想我写的一切都是正确的,但当我编译它时,我得到了错误: 错误1可访问性不一致:参数类型“eventStomper.RunEventArgs”的

我正在寻找一些帮助来了解事件。我一直在读关于它们的文章,看教程视频

我几乎能理解他们,但我总是遇到困难

我为自己制作了一个简单的WinForms测试应用程序来尝试和学习这个过程。在这个应用程序中,有两个行走的精灵在屏幕上奔跑。 当你点击表单时,它会创建一个坠落的小精灵(并创建一个事件)。步行者小精灵应该通过选择一条远离小精灵的新行走路径来对事件做出反应。我想我写的一切都是正确的,但当我编译它时,我得到了错误:

错误1可访问性不一致:参数类型“eventStomper.RunEventArgs”的可访问性不如委托“eventStomper.RunInFear”
错误2可访问性不一致:参数类型“eventStomper.RunEventArgs”的可访问性不如方法“eventStomper.Walker.RunAway(对象,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;
    }
}
namespace eventStomper
{
公共委托void RunInFear(对象发送方,RunEventArgs re);//用于响应事件的委托。
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
spawnWalkers();//创建两个walkers在窗体周围漫游
}
List thowmpList=new List();//thwomps的列表。这是为动画而迭代的。
List walkerList=new List();//与walker相同。
public void pictureBox1\u Click(object sender,EventArgs e)//当您单击背景时,它会生成一个thwomp
{
Point _spawnPoint=this.PointToClient(Cursor.Position);
Thwomp _Thwomp=new Thwomp(_spownpoint,sprite);//生成一个新的Thwomp
thowmpList.Add(_thwomp);//将其添加到thwomp列表中
_thwomp.TimeToRun+=walkerList[0]。失控;//向四处漫游的两个步行者注册。
_thwomp.TimeToRun+=walkerList[1]。失控;
//执行其他操作以设置thwomp精灵
}
}
公共类Thwomp
{
public int spriteX=0;//当前精灵位置
公共整数精神=0;
public int targetX=0;//thwomp将降落的位置。
public int targetY=0;
公共事件运行时间;
public void Animate()
{
//执行动画步骤。
}
公共Thwomp(点繁殖点、图片框精灵命名)
{
RunEventArgs re=新的RunEventArgs();
re._pointOfFear=繁殖点;
//设置thwomp精灵
TimeToRun(this,re);//触发事件。
}
}
公共班级步行者
{
public int spriteX=0;//当前精灵位置
公共整数精神=0;
公共步行者(点产卵点、图片框精灵命名)
{
//创造步行者
}
公共空间失控(危险点)
{
if(Math.Abs(sprite.Top-dangerPoint.Y)<20&&Math.Abs(sprite.Left-dangerPoint.X)<20)//如果靠近新创建的thcomp,则逃跑。
{
//选择一条远离危险的道路。
}
}
public void Animate()
{
//把助行器移开。
}
}
类RunEventArgs:EventArgs
{
公共点(公共点);;
}
}
我不知所措,因为一切都是公开的

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

参数类型“eventStomper.RunEventArgs”的可访问性不如委托“eventStomper.RunInFear”

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

public delegate void RunInFear(object sender, RunEventArgs re);
所以,这是公开的。到目前为止,一切顺利

class RunEventArgs : EventArgs 
{
    public Point _pointOfFear;
}
啊哈!这一个没有分配给的可访问性,这意味着-根据-它将默认为
内部

没有嵌套在其他类型中的顶级类型只能具有内部或公共可访问性。这些类型的默认可访问性是内部的


因此,将
RunEventArgs
class
public
,您的代码应该可以编译。

将这个
class RunEventArgs:EventArgs
更改为
public class RunEventArgs:EventArgs
,以处理编译错误。当我点击图片框时,它不会被触发。我得到一个错误“对象引用未设置为对象的实例”。因为当我尝试调用方法“TimeToRun(this,re);//触发事件”时,它表示TimeToRun是空对象。建议?