robocode環境でロボットを作ろうとしています。私の質問は、(たとえば) ロボット クラスの外でメソッド "fire()" を呼び出したい場合 (つまり、Robot を拡張し、run、onHitBybullet、... メソッドを持つクラス)、どうすればよいかということです。 ?
これは私が試したことの1つにすぎません(私の最新):
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の専門家ではないので、何か不足しているかもしれませんが、別のクラスを作成してロボットクラスを拡張しようとしました(したがって、ロボットメソッドを継承します)が、ロボットを拡張するクラスであるためJavaがエラーをスローしましたrun、onHitByBullet .. メソッドが必要です。