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

2017/1/24

Why only last command works

Marrryanx Marrryanx

2017/1/24

#
private void text()
    {
       if(getOneIntersectingObject(button1.class) != null) 
           {
               getWorld().addObject(new test(), 200 , 200);
            }
            else if(getOneIntersectingObject(button1.class) == null) 
             {
                 getWorld().removeObjects(getWorld().getObjects(test.class));
              } 
           else if(getOneIntersectingObject(button2.class) != null) 
           {
               getWorld().addObject(new test(), 200 , 200);
            }
            else if(getOneIntersectingObject(button2.class) == null) 
           {
               getWorld().removeObjects(getWorld().getObjects(test.class));
            } 
        }
button1 is in first world and button2 in in another world (2 different levels) just this part works
else if(getOneIntersectingObject(button2.class) != null) 
           {
               getWorld().addObject(new test(), 200 , 200);
            }
            else if(getOneIntersectingObject(button2.class) == null) 
           {
               getWorld().removeObjects(getWorld().getObjects(test.class));
            } 
danpost danpost

2017/1/24

#
Because all possibilities are covered by the first two condition (either the first one is true or, if not, the second one must be true), the last two conditions are never a consideration. Either use the same button class for both buttons (with possibly different text) or disconnect the 'if's by removing the 'else' at line 11 and check which world the actor is in before asking about each button.
You need to login to post a reply.