[英]I do not understand why when I run this code it never goes into the onScannedRobot(ScannedRobot e) method?
我正在使用Java中的环境Robocode,并试图创建一个与示例机器人Spinbot相对应的机器人。 我正在计算旋转机器人绕过的圆圈的中心,并以此为目标以获得击中旋转机器人的最佳机会。 我的代码可以很好地编译,但是当我运行它时,它永远不会进入onScannedRobot(ScannedRobot e)方法。 我通过在不同位置更改机器人的颜色进行了测试,可以说它从未进入过。
package LaurasRobot;
import robocode.*;
import java.awt.Color;
// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
/**
* LaurasRobot - a robot by (Laura)
*/
public class LaurasRobot extends Robot
{
private double x1;
private double x2;
private double x3;
private double y1;
private double y2;
private double y3;
private int count;
private double centerX;
private double centerY;
/**
* run: LaurasRobot's default behavior
*/
public void run() {
setColors(Color.red,Color.white,Color.blue); // body,gun,radar
// Robot main loop, moves the robot forward and back
while(true) {
ahead(100);
back(100);
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
setBodyColor(Color.yellow);//sets body color
//lets the gun, radar and body of the robot move indipendently
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
setAdjustRadarForRobotTurn(true);
if (count == 3)//creates to sample points to calculate the center of the cirlce with
{
count = 1;
x3 = e.getDistance()*(Math.cos(e.getBearing()));
y3 = e.getDistance()*(Math.sin(e.getBearing()));
}
if (count == 2)
{
count = 3;
x2 = e.getDistance()*(Math.cos(e.getBearing()));
y2 = e.getDistance()*(Math.sin(e.getBearing()));
}
else
{
count = 2;
x1 = e.getDistance()*(Math.cos(e.getBearing()));
y1 = e.getDistance()*(Math.sin(e.getBearing()));
}
while(y3 != 0.0)
{
setBodyColor(Color.blue);
if (count == 3)//creates to sample points to have an updated center
{
count = 1;
x3 = e.getDistance()*(Math.cos(e.getBearing()));
y3 = e.getDistance()*(Math.sin(e.getBearing()));
}
if (count == 2)
{
count = 3;
x2 = e.getDistance()*(Math.cos(e.getBearing()));
y2 = e.getDistance()*(Math.sin(e.getBearing()));
}
else
{
count = 2;
x1 = e.getDistance()*(Math.cos(e.getBearing()));
y1 = e.getDistance()*(Math.sin(e.getBearing()));
}
centerPoint();
double angle = angleGun(e);
turnGunRight(angle);//points the gun at the center of the circle that spinbot is makeing
fire(2);//fires one bullet at power 2
setBodyColor(Color.red);
}
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
back(10);
}
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
back(20);
}
//returns the midpoint of two numbers
public double midPoint(double x1 , double x2)
{
double midx1;
if (x1 > x2)
{
midx1 = ((x1 - x2)/2)+x1;
}
else
{
midx1 = ((x2-x1)/2)+x1;
}
return midx1;
}
//saves the center points in the instance variables
public void centerPoint()
{
double midx1 = midPoint(x1,x2);
double midx2 = midPoint(x3,x2);
// double midx3 = midPoint(x1,x3);
double midy1 = midPoint(y1,y2);
double midy2 = midPoint(y3,y2);
// double midy3 = midPoint(y1,y3);
centerX = (midy2- (newSlope2())*midx2-midy1+(newSlope1())*midx1)/( newSlope1() - newSlope2());
centerY = midy1 - (newSlope1())*midx1+(newSlope1())*(centerX);
}
//get the angle to move the gun and make it stay their
public double angleGun(ScannedRobotEvent e)
{
double meToCenter = Math.sqrt(((centerX - getX()) * (centerX - getX())) +((centerY - getY()) * (centerY - getY())));
double himToCenter = Math.sqrt(((centerX - x1) * (centerX - x1)) +((centerY - y1) * (centerY - y1)));
double angle = e.getBearing() - Math.cosh(((e.getDistance())*(e.getDistance())+(meToCenter)*(meToCenter)-(himToCenter)*(himToCenter))/(2*(e.getDistance())*(meToCenter)));
return angle;
}
//gets the perpendicular reciprocal of the lines connecting the first two points
public double newSlope1()
{
return (-1)/((y1-y2)/(x1-x2));
}
//gets the perpendicular reciprocal of the lines connecting the second two points
public double newSlope2()
{
return (-1)/((y3-y2)/(x3-x1));
}
//gets the perpendicular reciprocal of the lines connecting the third two points
public double newSlope3()
{
return (-1)/((y1-y3)/(x1-x3));
}
}
如果有人能告诉我我做错了/如何修复它,以便代码进入此方法,那就太好了,谢谢。
我不确定这是否已得到解决(我肯定希望在10个月后如此),但确实可供将来参考。 ScannedRobotEvent
仅在执行Scan()
方法或雷达波束移动时触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.