How to implement a Listener

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: imaqine, 99jonathan, roger

New User
Posts: 13
Joined: Tue Jul 12, 2011 4:39 pm

How to implement a Listener

Postby Tickstart » Fri Dec 23, 2011 7:28 pm

I have a major problem, I have no idea how to make my motors run without exhaustive hardware polling.
the following pseudo-code;

while (!touch.isPressed()) {

.. is just painful to implement, and I won't do it. How is the Motor.A.forward()-method supposed to work?? If Motor.A.forward() is all you write in a program, it will terminate immediately.

So I would guess Listeners would be a good solution, I haven't really used listeners much, so I don't know the syntax, but how would you implement the following pseudo-code?;


fairly straight forward I think. If you know a different way of implementing this without listeners that would be welcome too.


Posts: 381
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: How to implement a Listener

Postby roger » Sat Dec 24, 2011 6:57 am

Motor.A.forward() will start the motor rotating, then the method immediately returns. If this is the only line of your code, the program will exit and the motor will stop.
If you want the motor to run until a touch sensor is pressed, the simplest way to do it is

while (!touchIsPressed();

Not so painful, IMHO.
If your really don't like this, You could create a class that implements the SensorPortListener interface, but your main thread would need to keep going somehow till the stateChanged() method is called. All of which is probably more complicated than a loop that polls the sensor state in the main thread.

Good luck

New User
Posts: 13
Joined: Tue Jul 12, 2011 4:39 pm

Re: How to implement a Listener

Postby Tickstart » Mon Dec 26, 2011 9:32 pm

Roger that. I've managed to use the supplied listeners in a crude fashion, but some issues stil remain.. Thanks for letting me know how others solve this problem though, and happy new year!

Return to “NXJ Software”

Who is online

Users browsing this forum: No registered users and 5 guests