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

2014/11/12

Referring to another actor

Hersov Hersov

2014/11/12

#
Hi, I was wondering if it was possible to detect if another actor was clicked from a differant actor. What I mean is im in an actor called Display and I want it to detect when the dice was clicked. Is this possible and if so why does this code not work?
if (greenfoot.mouseClicked(Dice))
danpost danpost

2014/11/12

#
If you have a variable that referenced the dice, then:
// with variable or field
Dice dice; // holding the reference
// then
if (Greenfoot.mouseClicked(dice))
would be sufficient. Note: if 'Dice' in your code just refers to the class, then you are not using the method properly. It requires an Object instance (usually an Actor or World object) or 'null', not a Class. You could also, get the reference (provided you only had one dice in the world):
Dice dice = (Dice)getWorld().getObjects(Dice.class).get(0);
Another way would be to use:
if (Greenfoot.mouseClicked(null))
and get a MouseInfo object when true, so you can use 'getActor' (a MouseInfo method -- see the documentation) and then use 'instanceof Dice' to determine if the click was actually on a Dice object.
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null && mouse.getActor() != null && mouse.getActor() instanceof Dice)
Hersov Hersov

2014/11/12

#
Thanks alot Danpost, saving me <3
You need to login to post a reply.