简体   繁体   中英

onScannedRobot method not being called in Robocode

I am creating a simple robot in Robocode. I have a function walls shown below:

public void walls() {
        see = false;
        wallBool = true;
        double maxMove = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
        turnLeft(getHeading() % 90);
        ahead(maxMove);

        see = true;
        turnRight(90);
        turnGunRight((getHeading() - getGunHeading())+ 90);

        while (getEnergy() <= 115) {
            wallBool = true;
            see = true;
            ahead(maxMove);
            see = false;
            turnRight(90);
        }
    }

Note that see and wallBool are variables which have already been declared earlier in my code. In the while loop within the function, I call the turnRight(); method, which should automatically scan for other robots. However, my code does not run in my onScannedRobot function, included below:

public void onScannedRobot(ScannedRobotEvent e) {
    System.out.println("check");
    if (see==true) {
        System.out.println("check2");
        scan();
    }

    if (wallBool==true) {
        fire(2);
    }

Check is never printed to the console. What's wrong?

Any help would be greatly appreciated...

Please make sure you have put import robocode.ScannedRobotEvent; in the import. Also keep your public void onScannedRobot(ScannedRobotEvent e) just below public void run() function in case you have changed the order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM