2013-12-16 32 views
2

我正在寻求一些有助于理解事件的帮助。我一直在阅读关于他们的文章并观看教程视频。寻找关于让事件正常工作的建议

我几乎了解他们,但我一直在碰碰运气。

我使自己成为一个简单的WinForms测试应用程序来尝试和学习过程。在应用程序中,屏幕上有两个步行小精灵。 当你点击表单时,它会创建一个下降thwomp的精灵(并创建一个事件),步行者精灵应该通过选择一个离开精灵的新步行路径来对事件做出反应。我认为我已经正确地编写了一切,但是当我编译它时,出现以下错误:

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; 
    } 
} 
+1

将此类'RunEventArgs:EventArgs'更改为'public class RunEventArgs:EventArgs' – Silvermind

回答

5

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.

因此,使该RunEventArgspublic您的代码应编译。

+0

这会照顾编译错误。当我点击图片框时,它不会触发。我收到一个错误“对象引用未设置为对象的实例。”因为当我尝试调用方法“TimeToRun(this,re); //触发事件。”它是说TimeToRun是一个空对象。建议? – EtanSivad