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

2013/1/18

Count help, Please anyone!

Connman Connman

2013/1/18

#
I am working on a game and i want a player 1 screen to display after a certain amount of time, i figured that the easiest way to do this was with a simple count method, so i wrote one but it doesn't seem to work, i was wondering if anyone could look at the code and tell me if there is anything wrong or maybe a better way of doing it, thanks. Here is the code: public void textdisplay() { int textCount = 0; if (textCount == 0) { textCount ++; if (textCount == 100) { Greenfoot.setWorld(new Player1wins()); } } } oh and i would also like to know how to set images to animate my players, thanks.
danpost danpost

2013/1/18

#
The line int textCount=0; sets the value of a new local 'textCount' variable to zero. The next statement compares that same value to zero. Well, it was just set to zero; so, yeah, it is comparitively equal to zero. The next statement increments that value; so now it is equal to one. The following statement compares that value to one hundred. Well, no, it is absolutely equal to one, not one hundred. Therefore, the new Player1wins world will never activate. Upon exiting the method, all non-static variables that are local to the method are flagged for garbage collection. This is what will happen with the 'textCount' variable, as it was created within the method. You have just about all the elements needed to create a counter like you want here. There are only two things that need to be done: * the statement if(textCount==0) { should be if(textCount<100) { as we want to increment the counter as long as the value is still less than 100, not only when it equals zero; * the statement int textCount=0; needs to be moved to outside the method where it becomes a world instance field; this way the value is retained between method calls.
You need to login to post a reply.