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

2014/12/10

Boolean not working?

berdie berdie

2014/12/10

#
For some reason my boolean is True from second one i run the game not only when at the certain X and Y. I probably need to change the void but i dont know into where (i know void can't return).
public boolean Cargo = true;

public void act()  
 {
       if(!getWorld().getObjects(MainBoat.class).isEmpty()) {         
           Actor boat = (Actor)getWorld().getObjects(MainBoat.class).get(0);
           if (boat.getX()>=102 && boat.getX() <= 136 && boat.getY()==729){           
               Cargo = true;
           }
           else {
               Cargo = false;
            }
          
      }
    }   
public void BoatExit() {
    if(Cargo = true) {
       System.out.println("hje");
    }
    else {
        System.out.println("anus");
    }

}
classicjimmy classicjimmy

2014/12/10

#
Never assign values to a field variable in the field itself if you want it to change (if not you should add the keyword final before the return type). So remove the true assignment from public boolean Cargo and it should work properly.
public boolean Cargo;
davmac davmac

2014/12/10

#
Never assign values to a field variable in the field itself if you want it to change (if not you should add the keyword final before the return type). So remove the true assignment from public boolean Cargo and it should work properly.
This isn't right at all, it's fine to assign a value to a field in the declaration. berdie, the problem is line 17:
    if(Cargo = true) {  
This sets Cargo to true. You should instead have:
    if(Cargo == true) {  
or just:
    if(Cargo) {  
Also, Java convention is to have fields and methods begin with lower-case letters (so 'cargo' rather than 'Cargo').
berdie berdie

2014/12/10

#
ty for the reactions, the code won't still work if i try those things :S?
davmac davmac

2014/12/10

#
What do you mean by 'won't work'? I'm pretty sure the code works, it just might not do what you want it to :p
danpost danpost

2014/12/10

#
As is, when the actor of the class is created the boolean field will be true (line 1); then when a MainBoat is detected in the same world as this actor, the boolean field will be true if the detected actor is within the specified area and false if not. In other words it will continuously show the state of the MainBoat object being in that area. If the MainBoat object is removed from the world at any time with this actor still in it, the field will show whether the MainBoat object was in the area or not when removed. This is probably not the way you wanted to work this boolean field. For any help on the matter, you need to be explicitly clear on how this field is to be used.
berdie berdie

2014/12/11

#
The mainboat (which are 3 small boats). They're coming inside a harbor and get assigned a harbor dock to unload their cargo. With this piece of code i want that when they reach the dock and stop, that I get a system out print. I want that after 5 seconds the ships drives away from the harbor dock. I get the system out print when i do it direclty underneath the last if but the system out print still has to change to a function which moves the boat backwards and for some reason that wont work
danpost danpost

2014/12/11

#
I am somewhat confused about your code. About the initial code you posted, what class did that come from?
berdie berdie

2014/12/11

#
MainBoat extends Actor and underneath MainBoat i have different sizes of boats
berdie berdie

2014/12/11

#
I get that it is hard to understand i will try to make it clear. I need a function who returns True if the boat is at a certain location in the world
danpost danpost

2014/12/11

#
berdie wrote...
I get that it is hard to understand i will try to make it clear. I need a function who returns True if the boat is at a certain location in the world
Don't you mean you need a function for each boat that returns true if the boat is docked at the correct location? If the initial code is from the MainBoat class, then what exactly is line 6 trying to get a reference to? If you have more than one MainBoat in the world, how would you know which boat was returned from the first element in the list returned from using 'getWorld().getObjects(...)'? And, why is line 5 even being checked? Greenfoot calls the act method on the active world and all objects in that world repeatedly while the scenario is running. If no MainBoat objects were in the world, the act method in the MainBoat class would not be executing. Therefore the condition, as given, will always be true. Maybe you do not understand that any object created from a subclass of MainBoat is also considered a MainBoat object. That means all three of your boats are MainBoat objects.
berdie berdie

2014/12/11

#
danpost wrote...
berdie wrote...
I get that it is hard to understand i will try to make it clear. I need a function who returns True if the boat is at a certain location in the world
Don't you mean you need a function for each boat that returns true if the boat is docked at the correct location? If the initial code is from the MainBoat class, then what exactly is line 6 trying to get a reference to? If you have more than one MainBoat in the world, how would you know which boat was returned from the first element in the list returned from using 'getWorld().getObjects(...)'? And, why is line 5 even being checked? Greenfoot calls the act method on the active world and all objects in that world repeatedly while the scenario is running. If no MainBoat objects were in the world, the act method in the MainBoat class would not be executing. Therefore the condition, as given, will always be true. Maybe you do not understand that any object created from a subclass of MainBoat is also considered a MainBoat object. That means all three of your boats are MainBoat objects.
Danpost i already fixed it with isTouching and adding a counter to it. I know all my boats are extensions of MainBoat
danpost danpost

2014/12/11

#
Well, if there are any unresolved issues, what it involves plus any relevant code should be posted (even if it was posted previously -- especially if any changes were made to the code).
berdie berdie

2014/12/11

#
    int spawnCounter = 0;  

/** 
 *  Function counts till 5 when hitting the Dock1.class and than executes the MoveToExit
**/
public void ExitBoat()    
 {  
      if (isTouching(Dock1.class)){
          if (spawnCounter > 100) {  
          spawnCounter = 0;
          MoveToExit();
        }
        spawnCounter++;
        }

    }     
public void MoveToExit() {
      slow = -1;
      
}
The above code was the solution i got for my problem
You need to login to post a reply.