This site requires JavaScript, please enable it in your browser!
Greenfoot back
lex404
lex404 wrote ...

2012/2/22

change the color of a label in world when actor location changes?

1
2
lex404 lex404

2012/2/22

#
Hey, im working on some code to simulate a MIPS microprocessor. Im trying to change the color of a label when the getX of an actor equals a certain value. A snippet of the code is: this is in the world class:
if(getObjects(RedSquare.class).isEmpty())  
     {
       RedSquare RSquare = (RedSquare) getObjects(RedSquare.class).get(0);
       if(antTwo.getY() == 380)
          {
             ABusRight.setBackground(Color.RED);
           }
       }
i'm not receiving any errors, but its not changing the color like it should. any ideas?
danpost danpost

2012/2/22

#
Line 1: You should probably be asking the opposite of what you have here (use '!') Line 6: I am guessing 'ABusRight' is a reference to an actor. If so, note that 'setBackground(GreenfootImage image)' is a world class method, not an actor class method. And 'setBackground' does not have 'Color' as a parameter. If I am correct about 'ABusRight' being a reference to an actor, use
GreenfootImage img = ABusRight.getImage();
img.setColor(Color.RED);
img.fill();
ABusRight.setImage(img);
in place of line 6.
lex404 lex404

2012/2/22

#
i noticed an error when i was retyping the code above. but anyways. Actually ABusRight is a label. im trying to set the background color and text of the label based on the location of the Actor named RedSquare. so ABusRight is a label that is declared and initialized in the world, whereas RedSquare is an actor. so the function im trying to call is
if(RedSquare.getY==380)
{
   ABusRight.setBackground(Color.RED);
}
is there something im missing here? i thought if ABusRight (A label) and RedSquare (An Actor) are both public then there shouldnt be an issue with doing this function.
danpost danpost

2012/2/23

#
Is the 'setBackground(Color color)' a method in the 'ABusRight' type class? And, if so, what does it look like?
lex404 lex404

2012/2/23

#
yes. ABusRight is a Label from the Label Class. theres a setBackground and setText. the code is as follows,:
 public void setBackground(Color backgroundColor)
    {
        this.backgroundColor = backgroundColor;
        createImage();
    }


public void setText(String text)
    {
        this.text = text;
        createImage();
    }

t
the code that isnt working is
//this code right here is where the action should take place. but it doesnt//
           if(getObjects(RedSquare.class).isEmpty())
           {
              RedSquare RSquare=(RedSquare) getObjects(RedSquare.class).get(0);
                if(RSquare.getY()==400)
                {
                 ABusRight.setText("00002301");
                 ABusRight.setBackground(Color.RED);
                 BBusRight.setText("00000FFF");
                 BBusRight.setBackground(Color.GREEN);
                }  
           }
danpost danpost

2012/2/23

#
In the code that isnt working, change line 2 to read
if (!getObjects(RedSquare.class).isEmpty())
lex404 lex404

2012/2/23

#
thanks. that got me partly there. but im running into this funky little error. on line 5
if(RSquare.getY()==400)
that code wont work. but this one will
if(RSquare.getY()<400)
however the class RedSquare has the code:
  if(getX()==485 && (getY()<400) && (GSquare.getX()<485 || GSquare.getX()>582 ))
        {
            setLocation(getX(), getY()+1);
        }  
so the first code i posted on line 5 should work. but it doesnt. any ideas why it only works for the less than, but not the equal to?
danpost danpost

2012/2/23

#
Could the RSquare possibly have moved from Y=400 once it reaches there? From what you have given so far, it appears that RSquare will stop at Y=400, and the code to change the background color of the label should function. In order to delve further, I would need more information. Are you getting an error message, or are you just not getting the results you want to achieve? If you are getting an error message, what exactly does that message say (be detailed)? As a side note: in order that you are not constantly creating the new image each act() cycle, you may want to add a boolean insance variable 'private boolean foundAtY400 = false;' and when the object is found to be at y=400, change the value to 'true'. Oh, and use it to qualify changing the image ('if (!foundAtY400)'). I am assuming the once the RSquare reaches Y = 400 that it will no longer move at all -- is that correct?
lex404 lex404

2012/2/27

#
@danpost: no, im not getting an error message, its just not giving me the results i want. and yes you are correct, once RSquare reaches Y=400 it will no longer move at all.
danpost danpost

2012/2/27

#
There is insufficient information provided to assist at this time. As I said before, "In order to delve further, I would need more information."
lex404 lex404

2012/2/28

#
thanks for the help! im still not there yet though. when i attempted to do the boolean FoundAtY400, i got an error message saying that this function had to be public. so in the actor class for RedSquare, i put public boolean FoundAtY400= false; then i created an if statment in the same actor if(getY()==400) { FoundAtY400=true; } in the world class i changed line 5 of the code i posted above to if(RSquare.FoundAtY400=true) { some code. } the code works, to an extent. however, it occurs at the wrong time. it seems as if the FoundAtY400 is set to true automatically so its not being set once RedSquare reaches 400. any ideas? also, what other information could i offer that would be helpful in solving the issue? Thanks again for all of your help.
danpost danpost

2012/2/28

#
Actually, it may be because you have a single equal sign in 'if(RSquare.FoundAtY400=true)' instead of a double equal sign; but you can just ask 'if(RSquare.FoundAtY400)'.
lex404 lex404

2012/2/28

#
lol. it seems like no matter what we try it doesnt give the desired results. I didnt receive any error messages, but this time, the RSquare.FoundAtY400 wasnt recognized at all. when Rsquare reached 400, nothing happend. i tried using the double equal and if(RSquare.FoundAtY400) and got the same results with both. could the issue lie elsewhere and not on that specific part of the code?
danpost danpost

2012/2/28

#
Very possible. Could you upload the scenario with the source code so I could take a look? I can let you know when I have it, so you can immediately remove it from the site, if you wish.
lex404 lex404

2012/2/28

#
sure, its titled DataPath1.
There are more replies on the next page.
1
2