我正在尝试在 robocode 环境中制作机器人。我的问题是,如果我想(例如)在我的机器人类之外调用方法“fire()”(所以扩展 Robot 并具有运行的类,onHitBybullet,...方法),我该怎么做?
这只是我尝试过的事情之一(我的最新):
package sample;
import robocode.HitByBulletEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import sample.Interpretater;
public class MyFirstRobot extends Robot {
Interpretater inter;
public void run() {
intel = new Interpretator();
while (true) {
ahead(50); // Move ahead 100
//turnGunRight(360); // Spin gun around
back(50); // Move back 100
//turnGunRight(360); // Spin gun around
}
}
public void onScannedRobot(ScannedRobotEvent e) {
/*If I write fire() here, it will work, but I want to call it
from some other class (intel)*/
inter.onScan();
}
public void onHitByBullet(HitByBulletEvent e) {
turnLeft(90 - e.getBearing());
}
}
解释器代码:
包装样品;
public class Interpretator extends MyFirstRobot
{
public Interpretator(){
}
public void onScan(){
fire(1); //won't work, throws "you cannot call fire() before run()"
}
}
我根本不是Java专家,所以也许我遗漏了一些东西,但是我尝试创建另一个类并使其扩展我的机器人类(因此继承了Robot方法),但是由于扩展了Robot的类,Java抛出了错误需要运行,onHitByBullet .. 方法。