看来您实际上并没有访问传感器的值。请记住,您必须首先访问传感器的值(通过 getObstacleSensors(); 方法)。还记得此方法将每个传感器的值作为布尔数组返回。请参阅 javadoc:
getObstacleSensors public boolean[] getObstacleSensors()
将两个障碍物传感器的值作为 2 元素布尔数组返回。左侧传感器是第 0 个元素,右侧传感器是第 1 个元素。
返回: 2 元素数组中左右障碍物传感器的值
我最近做了一些类似于您尝试使用来自按钮操作侦听器的输入的操作。在实现它之前,我编写了一个名为“obstacleAvoidance”的方法,当遇到障碍物时机器人会实际执行该方法;此方法接受 Finch 对象(即 suzie)作为参数。这使得代码 [相对] 不那么混乱。执行此任务的代码(在动作侦听器中)可能如下所示:
私有类 ButtonListener 实现 ActionListener {
public void actionPerformed( ActionEvent e ) {
//Get the finchbot's obstacle sensors and store them in an array
boolean [] sensors = suzie.getObstacleSensors();
//Check to see if either of the ficnhbot's sensors
//detect an obstacle. This is enclosed in a while loop which is
//broken if either sensor returns false (detects an obstacle)
while (sensors [0] == false && sensors[1] == false)
{
// Sets the Action text field
System.out.println( "Performing Action..." );
// This method tells the robot to perform an action
command.performAction(suzie, -255, -85);
}
//Otherwise, perform obstacle avoidance maneuver
command.obtacleAvoidance(suzie);
}
}
我希望这对你来说是一个很好的起点......
吨