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

2017/1/26

Greenfoot Booleans?

098999 098999

2017/1/26

#
Hi, So I'm working on a game for my tech glass, and I wanted to make a small power up, where when you touch it with the player, it slows down the enemies. I have everything set up, except for the boolean for when you get the powerup. Here's my code: public boolean isLobsterFrozen() { if ( canSee(FreezeLobster.class) ) return true; else return false; } //this is on the player^ public void freeze() { if ( isLobsterFrozen (true) ) { lobsterMoveSpeed = 3; } } //this is on the enemy^ Thanks!
Super_Hippo Super_Hippo

2017/1/26

#
So you have a Player, a Lobster, and a power up FreezeLobster which slows down the Lobster. Right now, it looks like you only want to slow them while the player is on the power up. Wouldn't it be more useful to collect the power up and the effect would be permanent or for a specific time period? To compare a boolean, you can use one of the following, but not this 'isLobsterFrozen(true)' (as long as 'isLobsterFrozen' isn't a method which requires a boolean as a parameter...).
//a = name of the boolean

if (a)
//same as
if (a == true)

if (!a)
//same as
if (a == false)
Like you did with the canSee. The canSee method returns a boolean and as an if condition, it checks if it is equal to true.
danpost danpost

2017/1/26

#
You could use a public class (static) int field for the speed of all lobsters Then, you only need check and set it value from the enemy class. (no need for a boolean method). One problem with code given above is the word 'true' in the freeze method -- it should be removed. Try it out.
You need to login to post a reply.