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

2021/6/15

how do I call

Gabe1098 Gabe1098

2021/6/15

#
Ok so I'm new to greenfoot and I want to know how to do this. So you have
private void test() {

}
What I'm asking is how do I call that?
danpost danpost

2021/6/15

#
Gabe1098 wrote...
Ok so I'm new to greenfoot and I want to know how to do this. So you have << Code Omitted >> What I'm asking is how do I call that?
To call (or execute) the method, you write the following line:
test();
from within the class the method is located.
Gabe1098 Gabe1098

2021/6/15

#
Thanks! but how do I do a public one?
danpost danpost

2021/6/15

#
Gabe1098 wrote...
Thanks! but how do I do a public one?
Same way from within the class (for this one). That is, the above line is equivalent to:
this.test();
following the standard:
object.method();
where 'this' refers to the object the method is executing on (or for). Similar to the English grammar of a sentence -- subject followed by verb. From outside the class where the method is public, a reference to the object of the class the method is in is needed and must be explicitly given. For example:
if (isTouching(Block.class))
{
    // get reference
    Block block = (Block)getOneIntersectingObject(Block.class);
    block.crumble(); // calling 'public void crumble()' in Block class
}
The casting of type ,'(Block)', is required for the method to be found, as the getOneIntersectingObject method returns an Actor typed object, The type casting could, however be done like this:
Actor obj = getOneIntersectingObject(Block.class);
((Block)obj).crumble();
You need to login to post a reply.