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

2014/8/1

getObjectsAt using ||

davemib123 davemib123

2014/8/1

#
Is it possible to do something like this:
List heroFacingSign = getObjectsAt(321, 287, Hero.class) || getObjectsAt(160, 200, Hero.class);
I know they can be done separate.
lordhershey lordhershey

2014/8/1

#
I would put an invisible zone in front of the sign so if the user was facing a certain way and on that square or if they are facing a certain way and collide with the sign then the text would pop up.
danpost danpost

2014/8/1

#
I do not think that '||' (or'ing) is what you want to do; although if the first is empty you can assign it to the second:
List heroFacingSign = getObjectsAt(321, 287, Hero.class);
if (heroFacingSign.isEmpty()) heroFacingSign = getObjectsAt(160, 200, Hero.class);
That way, you would have one or the other or both (an empty list). If you want all Hero objects at both location in the list, you could use the following
List heroFacingSign = getObjectsAt(321, 287, Hero.class).addAll(getObjectsAt(160, 200, Hero.class));
Again, the list could be empty if no Hero object is at either location. This, 'and'ing, would be the same as 'or'ing if you only had one Hero object to begin with (providing that the Hero object cannot be at both places at one time).
davemib123 davemib123

2014/8/1

#
thanks for those responses. I will try them out. Here is a problem which links to the original post: I have tried this:
 private boolean readSign1 = false;
    public void checkMapInteractions()
    {
        List atOakLabSign = getObjectsAt(321, 287, Hero.class);
        List atOtherSign = getObjectsAt(160, 200, Hero.class);

        if(!readSign && !atOakLabSign.isEmpty())  
        {  
            System.out.println("at Oak's place");
            readSign = true;  
        }
        else if(atOakLabSign.isEmpty())  
        {  
            readSign = false;  
        }
        if(!readSign && !atOtherSign.isEmpty())  
        {  
            System.out.println("at second sign");
            readSign = true;  
        }
        else if(atOtherSign.isEmpty())  
        {  
            readSign = false;  
        }
but it keeps on displaying the message. Whereas in the secondary attempt it displays only one message at a time. but if I try it like this, it works fine:
 private boolean readSign1 = false;
    private boolean readSign2 = false;
    public void checkMapInteractions()
    {
        List atOakLabSign = getObjectsAt(321, 287, Hero.class);
        List atOtherSign = getObjectsAt(160, 200, Hero.class);

        if(!readSign1 && !atOakLabSign.isEmpty())  
        {  
            System.out.println("at Oak's place");
            readSign1 = true;  
        }
        else if(atOakLabSign.isEmpty())  
        {  
            readSign1 = false;  
        }
        if(!readSign2 && !atOtherSign.isEmpty())  
        {  
            System.out.println("at second sign");
            readSign2 = true;  
        }
        else if(atOtherSign.isEmpty())  
        {  
            readSign2 = false;  
        }
how can I get both if statements to read only 1 boolean?
danpost danpost

2014/8/1

#
You just need to combine your conditions (pseudo: if arriving at either sign, set readSign to true; if not at either sign, set readSign to false):
if (!readSign && (!atOakLabSign.isEmpty() || !atOtherSign.isEmpty))
{
    System.out.println("at one of the signs");
    readSign = true;
}
else if (readSign && (atOakLabsign.isEmpty && atOtherSign.isEmpty()))
{
    readSign = false;
}
danpost danpost

2014/8/1

#
In your first attempt, if at the OakLabSign, readSign will never be true because the second 'if' block (dealing with the OtherSign) will make it false again before the method ends. EDIT: missed empty set of parenthesis, '()', after 'isEmpty' on line 1 and line 6 above.
davemib123 davemib123

2014/8/2

#
great. thanks for showing a solution and explaining why my attempt wasnt working.
You need to login to post a reply.