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
danpost danpost

2012/2/28

#
OK, got it!
lex404 lex404

2012/2/28

#
thanks!
danpost danpost

2012/2/28

#
Alright, here is what I see. The buttonclicked method in the world is called only when the button is clicked. So, when the RSquare and GSquares are added to the world, it will check to see where RSquare is. BUT, after the method will not be called again until another click is registered. So, the check to see where RSquare is will not execute, but the one aforementioned time, because we are now on the next click, therefore next part of code. My suggestion is to see what happens if you move that segment of code to the act() method (which I believe is not present in the world class yet).
danpost danpost

2012/2/28

#
The segment I am referring to is this:
if (!getObjects(RedSquare.class).isEmpty())
{
    RedSquare RSquare=(RedSquare) getObjects(RedSquare.class).get(0);
    if(RSquare.FoundAtY400)
    {
        ABusRight.setText("00002301");
        ABusRight.setBackground(Color.RED);
        BBusRight.setText("00000FFF");
        BBusRight.setBackground(Color.GREEN);
    }  
}
danpost danpost

2012/2/28

#
Well, I tried it, and it did not seem to fix the problem! Still looking into it.
danpost danpost

2012/2/28

#
Yeah, it did! Make the adjustment mentioned above to the world class, and in the RedSquare class, change '<' to '==' in the 'if(getY()<400)' statement.
lex404 lex404

2012/2/28

#
brilliant! thanks danpost! now that you pointed it out it definitely makes sense. so i added an act() method, moved the code down and it works like it should. once i finish the final version i will upload it so others can use it and hopefully gain some insight as i did. thanks again!
danpost danpost

2012/2/28

#
Often, in scenarios such as this, I would create a world instance integer variable called 'faze' (for phase), and use a switch in the act() method for processing at each phase. In your case, it would end up something like: faze = 0: if (!buttonClicked()) return; fetch(); faze = 1; return; faze = 1: if (!buttonClicked()) return; load(); faze = 2: pausebutton(); return; faze = 2: if (!squaresStopped()) return; setALU(); faze = 3; activatebutton(); return; faze = 3: if (!buttonClicked()) return; process(); faze = 4; return; faze = 4: if (!buttonClicked()) return; unload(); faze = 5: pausebutton(); return; faze = 5: if (!squareStopped()) return; faze = 6; activatebutton(); return; faze = 6: if (!buttonClicked()) return; save(); faze = 7; return faze = 7: return; with methods created for the different actions. (no 'break;' statements are needed as everything 'return;'s) I do not know if this would have been feasable in your case, as you use the 'actionlistener'; though, it could be adjusted to work with it.
You need to login to post a reply.
1
2