1

So I've been working on this robot in Robocode, and I want it to change its color based on the result of the command getEnergy()

However, I can't seem to get it to work. The java code I have created is the following:

    getEnergy();
    if(getEnergy()>=90)
    {
        setBodyColor(new Color(0, 255, 0));
    }if(getEnergy()<90 && getEnergy()>=40){
        setBodyColor(new Color(0, 0, 255)); 
    }if(getEnergy()<40){
        setBodyColor(new Color(255, 0, 0)); 
    }

I also tried this:

    getEnergy();
    if(getEnergy()>=90)
    {
        setBodyColor(new Color(0, 255, 0));
    }else if(getEnergy()>=40){
        setBodyColor(new Color(0, 0, 255)); 
    }else if(getEnergy()<40){
        setBodyColor(new Color(255, 0, 0)); 
    }

What am I doing wrong?

Tanno
  • 93
  • 1
  • 5

1 Answers1

2

Those code snippets look reasonable. (Though the first line getEnergy(); is doing nothing.) Two things come to mind as to why you might not see the colors changing.

  • Is this code actually being executed each tick? For instance, just having this at the top of your run() method would not be sufficient, as it is only called at the start of each round. It would need to be in a loop within run() that's calling execute() each tick, or in something that is executed frequently like onScannedRobot(). You could add System.out.println(getTime()) to those blocks and watch the output console to help troubleshoot.
  • IIRC, there is an option in the Robocode settings to allow or disallow robots changing their colors during the match. It might be disabled by default.
Voidious
  • 41
  • 2
  • is right, Robocode only lets' you set color once during battle. That must be the reason you're not able to see this change. – karora May 19 '16 at 15:16