我正在开发一款游戏,当记号为'x'时,它会执行操作。我有一个TickEvent委托,它是一种在需要时运行的方法。
public delegate void TickEvent(object[] objects);
我有一个计划程序,可以在特定的时刻运行该方法。
public static void scheduleTick(long tick, TickEvent tickEvent)
{
queue.put(tick, tickEvent);
}
还有一个处理程序。
public static void handleScheduledTicks()
{
for(int i = 0; i < queue.variables.Count; i++)
{
if((long)queue.variables[i] < elapsedTicks)
{
var tickEvent = (TickEvent)queue.values[i];
// runs the tickEvent with its arguments
queue.remove(i);
}
}
}
进行活动:
public static void someEvent(object[] objects)
{
Console.WriteLine(objects[0].ToString());
}
Calling the eventHandler and scheduling the event:
static long ticks = 0;
public static void Main()
{
scheduleTick(100, someEvent(new object[]() {"some args"})
while(true)
{
handleScheduledTicks();
ticks++;
}
}
所以我的问题是如何运行带有其参数的tickEvent? (handleScheduledTicks评论行)
答案 0 :(得分:0)
我觉得下面一行本身有问题:
public static void Main()
{
scheduleTick(100, someEvent(new object[]() {"some args"})
/* this will not complile. you are passing `void` to method `handleScheduledTicks` as an argument to type of TickEvent. It should be like :
scheduleTick(100, someEvent) */
while(true)
{
handleScheduledTicks();
ticks++;
}
}
并且handleScheduledTicks
部分应该是:
if((long)queue.variables[i] < elapsedTicks)
{
var tickEvent = (TickEvent)queue.values[i];
/* tickEvent(new object[]() {"some args"}); */
queue.remove(i);
}
答案 1 :(得分:0)
我想我找到了解决方法:
tickEvent(tickEvent.Method.GetParameters());