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

2016/5/8

Error trying to use "intersects"

ggongwer ggongwer

2016/5/8

#
Hello, I am trying to use the intersects(Actor other) method in an Actor in a World, but keep getting the message intersects(Greenfoot.Actor) has protected access in Greenfoot.Actor I am using it in a subclass of Actor, and I thought that was okay for a protected method. Am I misunderstanding "protected"?
ggongwer ggongwer

2016/5/8

#
Never mind, Greenfoot friends. I figured it out :)
danpost danpost

2016/5/9

#
ggongwer wrote...
Never mind, Greenfoot friends. I figured it out :)
To clarify, 'protected' means you can only explicitly call the method on 'this'. That is,
// valid are
if (this.interects(someActor)) // explicit object
// and
if (interects(someActor)) // implicit object (still 'this')
// and this may even work
Actor thisActor = this;
if (thisActor.intersects(someActor))

// invalid are
if (someOtherActor.intersects(someActor))
// and
if (someOtherActor.intersects(this))
ggongwer ggongwer

2016/5/10

#
Thanks, DanPost!
danpost danpost

2016/5/10

#
danpost wrote...
To clarify, 'protected' means you can only explicitly call the method on 'this'.
I guess this is not entirely accurate. You can call a protected method on any object that is created from that particular class or a subclass of it. So, if you call the 'intersects' method from a class called 'SomeActor', then it must be called on an object of type SomeActor:
// valid in SomeActor class
Object object = getWorld().getObjects(SomeActor.class).get(0); // assume at least one is in the world
SomeActor someActor = (SomeActor)object;
boolean touchesSomeOtherActor = someActor.intersects(SomeOtherActor.class);

// using this for the last line will not compile even though the object is a SomeActor object
boolean touchesSomeOtherActor = object.intersects(SomeOtherActor.class);
ggongwer ggongwer

2016/5/11

#
Thanks for clarifying. I was trying to call it from a sibling class, and that's what generated the error.
You need to login to post a reply.