View Full Version : Exception with AddMouseButtonListener

10-30-2011, 08:35 AM
We're new to Alice, have gone straight into 3.0 beta and are having issues that may either be newbie trouble or a bug in beta.

Trying to get a bunch of robots to move towards a target, and when each robot is clicked by the mouse it jumps back a way.

To do this, we've defined an array of the robots, and for each robot 'm' in the array we do m.AddMouseButtonListener, and specify an action m.MoveAwayFrom the target.

Then, in a seperate block, for each robot we have a While(true) that moves the robot towards the target and then has a delay, so they keep inching forward.

If we just have the move loop on its own things are fine, the robots move towards the target step by step. If we have the AddMouseButtonListener block, either as part of a single For Each loop that does everything or as part of an initial For Each loop, there's repeated exceptions fired when the program runs.

Is there something obvious we're doing wrong here? Are we only allowed mouse button listeners on one object? I've done a search on the forum for AddMouseButtonListener and had no hits.

10-31-2011, 01:09 PM
Ah. There was a vaguely obvious thing.

If you have

for (MyRobot m:robots)
m.addMouseButtonListener ( whatever )

the variable m isn't valid in "whatever". It seems you have to use e.getModelAtMouseLocation instead. But e isn't available from the list of things you can get Procedures and Functions for, as far as I can see, so you have to drop in the procedure you want from some other object- which of course would logically be "m", and then you have to use the drop down list in the code window to change "m" to "e" and pick the right function.

Replied to self because nobody else seemed to be and it might be useful to someone at some point :)