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

2017/5/19

How to compare Greenfoot Actors

1
2
Hi, I use the getOneIntersectingObject method to check if my player hits a cloud
private Actor a;
a= getOneIntersectingObject(cloud.class);
If I now have "a" I'd like to compare it, if it's really a cloud.. What do I have to do this or is this comparison not needed Thanks for help
danpost danpost

2017/5/19

#
The 'getOneIntersectingObject(cloud.class)' code will return either a cloud object or 'null'. To check to see if a cloud was intersecting and referenced, use:
if (a != null)
Yehuda Yehuda

2017/5/19

#
Look at the arguments you put in for the method, it says 'cloud.class' so it will only return clouds. If you want you can do the following (which is very unnecessary, but will accomplish the same thing):
Actor actor = getOneIntersectingObject(Actor.class);
if (actor instanceof Cloud) {
    // I capitalised the 'C' in "Cloud" because the names of classes are meant to be
}
I'm not sure if first have to check if it's null.
danpost danpost

2017/5/19

#
Yehuda wrote...
If you want you can do the following (which is very unnecessary, but will accomplish the same thing): < Code Omitted > I'm not sure if first have to check if it's null.
Either one or the other (not both) is fine unless you want to do different things depending on the type of cloud (when you have one or more subclasses of the cloud class). Then, you need to use the 'instanceof' boolean operator.
Yehuda Yehuda

2017/5/19

#
danpost wrote...
Either one or the other (not both) is fine.
Are you talking to me? "one or the other" what two things is this referring to? I was showing that if you put Actor.class as the argument you can then check to see if it's a cloud by using 'instanceof'. (But I still said that it's unnecessary.)
danpost danpost

2017/5/19

#
Yehuda wrote...
danpost wrote...
Either one or the other (not both) is fine.
Are you talking to me?
I was talking to both (or anyone who reads this). You had said:
Yehuda wrote...
I'm not sure if first have to check if it's null.
So, to be sure, I gave my input. You do not have to check for null before checking 'instanceof'. If 'actor' is 'null', 'instanceof' will return 'false' for any class without error.
Yehuda Yehuda

2017/5/19

#
danpost wrote...
You had said:
Yehuda wrote...
I'm not sure if first have to check if it's null.
So, to be sure, I gave my input. You do not have to check for null before checking 'instanceof'. If 'actor' is 'null', 'instanceof' will return 'false' for any class without error.
Oh, I just wasn't sure if you can compare a null instance.
davmac davmac

2017/5/20

#
Yehuda wrote...
Look at the arguments you put in for the method, it says 'cloud.class' so it will only return clouds. If you want you can do the following (which is very unnecessary, but will accomplish the same thing):
Actor actor = getOneIntersectingObject(Actor.class);
if (actor instanceof Cloud) {
    // I capitalised the 'C' in "Cloud" because the names of classes are meant to be
}
This is functionally different: it checks for an intersecting object, and then checks if it is a cloud. The original form checks for an intersecting cloud object. The important difference is that the original won't be confounded in the presence of other types of object. That is, suppose your actor is intersecting both a cloud and a bird. The original form:
a= getOneIntersectingObject(cloud.class);
... will always return the cloud. If you instead use getOneIntersectingObject(Actor.class), it might return either object. If it returns the bird, you will not detect the presence of the cloud.
Yehuda Yehuda

2017/5/21

#
@davmac So you're just saying that what I gave isn't the same as setting cloud.class as the argument. (But I think it will still accomplish the same thing, just if I only check for the instance of 'actor'/'a' then it will take at least two act cycles to get the bird and the cloud (depending if the bird and the cloud are both still there).
danpost danpost

2017/5/21

#
Yehuda wrote...
if I only check for the instance of 'actor'/'a' then it will take at least two act cycles to get the bird and the cloud (depending if the bird and the cloud are both still there).
The 'getOneIntersectingObject' method will return the first object it finds intersecting the player -- every time. In other words, it could return the same object for many act cycles before a change occurs. Just because it did not return a specific intersecting object one time does not mean it will the next, even if there are only two intersecting objects. In fact, it will probably return the same actor until that actor no longer intersects or until a different actor starts to intersect that the method finds quicker.
davmac davmac

2017/5/21

#
Yehuda wrote...
But I think it will still accomplish the same thing, just if I only check for the instance of 'actor'/'a' then it will take at least two act cycles to get the bird and the cloud (depending if the bird and the cloud are both still there).
There's no guarantee that you will see both objects, because getOneIntersectingObject doesn't make any such guarantee. Even if both bird and cloud objects were intersecting for several cycles, you might only ever see one of them.
Yehuda Yehuda

2017/5/21

#
@danpost @davmac Both of you are telling me the same thing, and both of you are telling me the thing which I already said. I didn't think the actor the method returns changes each act cycle, that's why I said:
Yehuda wrote...
...if I only check for the instance of 'actor'/'a' then it will take at least two act cycles to get the bird and the cloud (depending if the bird and the cloud are both still there).
If you would have noticed these words then you would have known that your (plural) last replies were unnecessary. (I'm not always wrong.)
danpost danpost

2017/5/21

#
@Yehuda, your precursor to that 'if' statement was:
Yehuda wrote...
But I think it will still accomplish the same thing, ...
What we are trying to tell you is that you will be very fortunate to have it accomplish the same thing (when using 'Actor.class', that is). And if by chance you knew that also, then there would be absolutely no reason for you to suggest it to begin with.
Yehuda Yehuda

2017/5/21

#
@danpost When I suggested it I didn't think of it, but I was already enlightened by davmac's first post, no need for all the other posts. (See my first sentence "So you're just saying that what I gave isn't the same as setting cloud.".)
danpost danpost

2017/5/21

#
Yehuda wrote...
@danpost When I suggested it I didn't think of it, but I was already enlightened by davmac's first post, no need for all the other posts. (See my first sentence "So you're just saying that what I gave isn't the same as setting cloud.".)
Sorry, I do not see it that way. You had still shown some doubt and was thinking incorrectly when you said:
But I think it will still accomplish the same thing, just if I only check for the instance of 'actor'/'a' then it will take at least two act cycles to get the bird and the cloud (depending if the bird and the cloud are both still there
(taking that within context of the rest of that post). My post immediately following that was not only to ensure you that what you said was incorrect (or, at minimum, misleading), but was to confirm to GreenfootNewbie2000 that he should not go that route and stick with using 'cloud.class (or Cloud.class, if it was changed to comply with convention).
There are more replies on the next page.
1
2