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

2014/5/3

Resetting boolean

applefan57 applefan57

2014/5/3

#
Hi, I'm quite new to Greenfoot. I am making a turn based game where counters have to move up a board, after a dice has been rolled, and I'm trying to make the counters only move only once when they are clicked on- at the moment a player can roll a dice and then move they counter as many times as they like- whereas I want the player only to be able to move their counter once. To combat this, I have initiated a boolean called hasMoved which is set to false when a player hasn't moved, but is turned true once the counter has moved. This has solved the problem, but it now means that after one turn, the counters cannot move anymore. How do I go about resetting this boolean after the counter has moved without allowing the counter to move more than once?
applefan57 applefan57

2014/5/3

#
By saying counters, I mean 'tokens' or 'pieces'- to avoid confusion!
danpost danpost

2014/5/3

#
Maybe you need to initiate the boolean on 'hasRolled' instead of 'hasMoved'. if not hasRolled, roll (set hasRolled to true) if hasRolled, move (set hasRolled to false)
applefan57 applefan57

2014/5/3

#
It only seems to work once, as after that the booleans are stuck on the same value- where should i reset the booleans without resetting them too early and allowing the player to move twice in the same roll?
danpost danpost

2014/5/3

#
applefan57 wrote...
It only seems to work once, as after that the booleans are stuck on the same value- where should i reset the booleans without resetting them too early and allowing the player to move twice in the same roll?
You will need to post the code you are attempting to use.
applefan57 applefan57

2014/5/4

#
public static boolean hasMoved = false; public void act() { if (PlayerTurn.PlayerOne== true && hasMoved== false){ if (Greenfoot.mouseClicked(this)){ if ( Dice.roll==1){ setLocation(getX(), getY()-1); PlayerTurn.PlayerOne = false; hasMoved = true; } if ( Dice.roll==2){ setLocation(getX(), getY()-2); PlayerTurn.PlayerOne = false; hasMoved = true; } if ( Dice.roll==3){ setLocation(getX(), getY()-3); PlayerTurn.PlayerOne = false; hasMoved = true; } if ( Dice.roll==4){ setLocation(getX(), getY()+1); PlayerTurn.PlayerOne = false; hasMoved = true; } } } }
applefan57 applefan57

2014/5/4

#
That was just the Player one Counter code
danpost danpost

2014/5/4

#
What code are you using to tell the dice to roll? What controls the dice (not necessarily that which is in the Dice class)? How does the player roll the dice, I guess is what I am asking?
applefan57 applefan57

2014/5/4

#
they click a roll dice button, which then causes a random number generator to run
danpost danpost

2014/5/4

#
I am pretty sure that if you set 'Dice.roll' to zero after a move that it will improve the situation. You probably will NOT need the 'hasMoved' boolean along with it.
applefan57 applefan57

2014/5/5

#
This worked thank you, although I still used the boolean; I set it to false if Dice.roll = 0, and I made Dice.roll 0 after every roll, thank you!!
You need to login to post a reply.