I am teaching a class in Greenfoot, and I am stuck over something that seems trivial.
I need the option, that whenever someone drags an actor in my world, then drops it a method is triggered. How is this done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Simply override the
addedToWorld()method on your actor. The following example for instance defines an actor that always positions itself at a position of 50,50 when inserted into the world:The
setLocation()call is of course arbitrary, whatever code you provide in that method will be executed as soon as the actor has been added to the world (whether interactively via drag / drop, or programmatically.)As a side note, you can also put some initialisation code in the actor’s constructor, but since this happens before the actor has been placed into the world, any code setting the actor’s location, rotation, etc. will throw an exception.