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

2011/9/17

If Statement Multiple Conditions

LastingLuck LastingLuck

2011/9/17

#
Right now I have code for a counter that counts down by one each act till 0, then I need it to reset after I press a certain key but only if the counter is at 0. So something like "if counter = 0 and isKeyDown("x")". Is this possible?
ez4u2c ez4u2c

2011/9/17

#
Total n00bE here, but I think that the syntax would be something like:
if ((counter == 0) && Greenfoot.isKeyDown("x"))  {
    doStuff()
}
LastingLuck LastingLuck

2011/9/17

#
I tried what you suggested, but I get an error that says "Operator && cannot be applied to int,boolean"
LastingLuck LastingLuck

2011/9/17

#
I just found a work around by putting the "if counter = 0" inside another if statement, so it looks like this
        if (Greenfoot.isKeyDown("x"))
        {
            if (counter = 0)
            {
                counter = 500;
            }
        }
If anyone knows a better solution, please tell me
davmac davmac

2011/9/17

#
Whoa, whoa - "if (counter = 0) " - that should never work, you'd need: "if (counter == 0)", a double-equal sign rather than a single one. And, what ez4u2c was correct and should have worked - perhaps you type it incorrectly? (In particular - perhaps you used '=' where '==' was required?)
LastingLuck LastingLuck

2011/9/17

#
I changed it to what you said, and it worked perfectly. I'm still very new to programming so thanks a lot for the help.
You need to login to post a reply.