I'm trying to make a robot in the robocode environment. My question is, if I want (for example) to call the method "fire()" outside of my robot class (so the class that extends Robot and has the run, onHitBybullet, ... methods), how do I do it?
This is just one of the things I tried (my latest):
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());
}
}
Interpreter code :
package sample;
public class Interpretator extends MyFirstRobot
{
public Interpretator(){
}
public void onScan(){
fire(1); //won't work, throws "you cannot call fire() before run()"
}
}
I'm not an expert in java at all, so maybe I'm missing something, but I tried creating another class and making it extend my robot class (therefore inheriting the Robot methods) but then java threw errors since a class that extends Robot needs the run, onHitByBullet .. methods.