事件#

初始化事件类#

使用以下构造函数创建事件:

The event(callback) constructor uses two parameters:

范围

描述

callback

先前定义的函数,当事件广播时将作为线程被调用。

// Define the function print()
void print(){
  brain.screen.print("Event was broadcast");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Construct an Event "Event" with the event class.
  event Event = event(print);
}

This Event object will be used in all subsequent examples throughout this API documentation when referring to event class methods.

类方法#

set()#

The set(callback) command sets a new callback and arguments for the Event.

参数

描述

打回来

事件触发时调用的新回调函数。

**返回:**无。

broadcast()#

The broadcast() command broadcasts the event and causes all registered callback functions to run.

**返回:**无。

void runOnBroadcast1() {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Broadcast1 Running");
}

void runOnBroadcast2() {
  Brain.Screen.setCursor(2, 1);
  Brain.Screen.print("Broadcast2 Running");
}

int main() {

  // Register callback functions to event.
  Event(runOnBroadcast1);
  Event(runOnBroadcast2);
  
  // Brief wait to ensure event is registered
  wait(15, msec);

  // Broadcast the event.
  Event.broadcast();
}

broadcastAndWait()#

The broadcastAndWait() command broadcasts the event, causes all registered callback functions to run, and waits until all callback functions have been completed.

**返回:**无。

void runOnBroadcast1() {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Broadcast1 Running");
}

void runOnBroadcast2() {
  Brain.Screen.setCursor(2, 1);
  Brain.Screen.print("Broadcast2 Running");
}

int main() {
  // Register callback functions to event.
  Event(runOnBroadcast1);
  Event(runOnBroadcast2);
  
  // Brief wait to ensure event is registered
  wait(15, msec);

  // Broadcast the event and wait for completion.
  Event.broadcastAndWait();

  Brain.Screen.setCursor(3, 1);
  Brain.Screen.print("Listeners finished running");
}