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

2014/8/22

Priniting

1
2
danpost danpost

2014/8/22

#
With turn-based scenarios, I usually use an int field to track the phase of the game. Each action is given a number and when an action is completed, the number is incremented and the next step in the game is done. For example, my backgammon game (which is not up on the site) has something like the following: * SETUP phase = -1 ; proceeds to 0; * MOVE POINTER TO DIE AND WAIT FOR CLICK phase = 0 ; proceeds to 1; * COMENCE DIE ROLLING phase = 1 ; proceeds to 2; * WAIT FOR ROLLING DIE TO STOP phase = 2 ; proceed to 3; * TURN EXECUTION phase = 3 for player or = 4 for AI ; proceed to 0; * AI MOVE POINTER TO COUNTER phase = 5; proceed to 6; * AI DRAGS COUNTER TO DESTINATION = 6; proceed to 7; * WAIT FOR DRAG TO COMPLETE phase = 7; proceed to 0; The field is basically used to determine what to do and when. A 'switch' statement controls the operations from within the world 'act' method.
applefan57 applefan57

2014/8/22

#
Was the first way just to make delay an integer with a value of the amount of seconds of delay and then add it to the code like this:
Greenfoot.delay(300)setImage(new GreenfootImage(PlayerNameInput.text2+", you cannot move this counter" , 20, Color.WHITE, Color.BLUE));
I'm sorry for being useless but I don't quite understand how to add the delay into the class. To clear things up, It is a turn based game whereby each player (Player 1 and Player 2) roll a dice (1-4) and move counters up accordingly but can only take one move and cannot move off the board, and I am trying to code a message for when they try to move off the board, but I can't work out how to make the message stay long enough(I think it's because the other print statements in the class override this print statement so that it only prints when the object is clicked). How is the integer recognised as a form of time?
applefan57 applefan57

2014/8/22

#
the thing is that the game is basically finished apart from this print message, and I don't want to recode the whole game as I sense that there is a simple fix to allow the message to print for a longer time. I was trying to keep the game simple with little chance of error, and the actual movement is ok, it's just the printing of the message which has gotten me stuck!
danpost danpost

2014/8/22

#
NVM. What I had here will not work. The mouse click will not get registered. Let me ponder some more.
danpost danpost

2014/8/22

#
You could add a boolean field to the class:
private boolean waitingOnClick;
in the act method, start with these line:
if (waitingOnClick && Greenfoot.mouseClicked(this)) waitingOnClick = false;
if (waitingOnClick) return;
Then, after setting the message, add these line:
waitingOnClick = true;
return;
Just thought of something else. You may have to put this line between the method calls in your act method as well:
if (waitingOnClick) return;
so that when any methods sets the wait, the other methods do not run and alter the text.
applefan57 applefan57

2014/8/22

#
Ahh ok thanks, and I can just print the message when the Boolean is true, I'll try that thank you!
danpost danpost

2014/8/22

#
applefan57 wrote...
Ahh ok thanks, and I can just print the message when the Boolean is true, I'll try that thank you!
No. You set the boolean to true when you print the message. I wanted to say that you need to place this line:
if (waitingOnClick) return;
in the act method ONLY after any method that you set the boolean to true in (besides the second line of the act method).
applefan57 applefan57

2014/8/22

#
But I would have to set the Boolean from each of the two player one counter classes when they are clicked?
Super_Hippo Super_Hippo

2014/8/22

#
Greenfoot.delay(...) will also work, but only if you don't want anything to happen in a certain time and this is probably not the case. This is what I meant:
setImage(new GreenfootImage(PlayerNameInput.text2+", you cannot move this counter" , 20, Color.WHITE, Color.BLUE));
delay = 300;
danpost danpost

2014/8/22

#
applefan57 wrote...
But I would have to set the Boolean from each of the two player one counter classes when they are clicked?
No. Just in this class.
applefan57 applefan57

2014/8/23

#
Thank You, the green foot .delay seemed to work, and The time of 200 was fine, as it allows enough time to read the message but doesn't delay too long. Thank you danpost and Super_Hippo so much!!!!
You need to login to post a reply.
1
2