[Answered] Using an if statement to switch a boolean's value
Spoiler for Original post:
Well I'm trying to add a boolean "hammered" that checks to see if a player is using a hammer, or uses an item on a hammer. Here is what I have for the code
For example, in that line my netbeans tells me that hammered is undefined. Does anyone know how to fix this? Both if statements are under the handlePacket void in my ItemOnItem class.
Edit: It also says hammered is a variable not a boolean, and in the first if statement it says variable hammered is unused.
Edit2: I can also use anything on the box to get it to open, for example some earth runes, or a fighter torso :| does anyone know how to keep this from happening? I think that in my if hammered == true I'm making hammered equal true for the whole if statement :'(
Edit3: BIG EDIT HERE.
Ok I got the boxes to stop opening when random items were used on them (edit2) now I need them to open when hammers are used on them. Here is my current code...
Code:
boolean hammered = false;
public void handlePacket(Player player, int packetId, int packetSize) {
if (player == null)
return;
int usedWith = player.stream.readSignedWordBigEndian();
int itemUsed = player.stream.readSignedWordA();
PlayerItems pi = new PlayerItems();
Now, I want that boolean at the top hammered to become true when item 2347 (a hammer) is used or used with. And because the boolean hammered will be true this should cause the box to open.
Fixed it myself, thanks for at least trying Vincent, and thank you to Method for telling me I needed to declare the boolean outside of the void.
Heres how I got it to work, the condensed method anyways.
Code:
boolean hammered = false;
public void handlePacket(Player player, int packetId, int packetSize) {
if ((itemUsed == 2347) || (usedWith == 2347)) {
hammered = true;
You declare the boolean outside of everything, just underneath public class BlahBlah {
Then inside of your void you want to add the boolean switch to you add,
Code:
if(condition for boolean to be true) {
yourBooleanHere = true;
}
No conventions, lol but you see how it works. Hope this helps anyone searching for an answer to a similar problem in the future.
06-22-2010, 03:53 PM
Method
You need to declare hammered in a place where the scope of the variable encapsulates all of the if statements that access it.
06-22-2010, 08:30 PM
Mr Steve
So would that be before the packetHandler void? I'm going to try there now :p
Edit: Adding in
public boolean hammered;
before the handlePacket void makes my hammered in first code at the top of the page say it is hiding a field.
I also tried private boolean and just boolean, neither worked.
06-22-2010, 09:00 PM
Vincent
are you perhaps trying to do this? why are you making 4 if statements and a boolean if you can easily do it with 1-2 if statements
Thats what I have, I'm trying to replace two of those if item = x with the hammer thing. Can you explain why my ifs don't make sense?
Edit: If the item isn't equal to 2347 hammer should stay false, so I should take that one out right? I thought I had false there.
Edit Again: What if I made it into this?