- Code: Select all
import josx.robotics.*;
import josx.platform.rcx.*;
public class nRobot {
boolean f=true,c=true,s;
public class goForward implements Behavior {
public boolean takeControl() {
return true;
}
public void suppress() {
Motor.A.stop();
Motor.C.stop();
}
public void action() {
Motor.A.setPower(3);
Motor.C.setPower(3);
Motor.A.forward();
Motor.C.forward();
Arbitrator a = new Arbitrator(new Behavior[]{
new nRobot.goCrazy()
});
a.start();
}
}
public class goCrazy implements Behavior {
public boolean takeControl() {
return true;
}
public void suppress() {
Motor.A.stop();
Motor.C.stop();
}
public void action() {
// Back up:
Motor.A.setPower(4);
Motor.C.setPower(4);
Motor.A.backward();
Motor.C.backward();
try{Thread.sleep(1000);}catch(Exception e) {}
// Rotate by causing only one wheel to stop:
Motor.A.stop();
try{Thread.sleep(500);}catch(Exception e) {}
Motor.C.stop();
}
}
public class doSound implements Behavior {
public boolean takeControl() {
return true;
}
public void suppress() {
// Nothing to suppress
}
public void action() {
play();
try{Thread.sleep(3000);}catch(Exception e) {}
System.exit(0);
}
public void play() {
for(int i=0;i<10000; i+=5) {
Sound.playTone((i*100), i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {}
}
}
}
public static void main(String [] args) {
nRobot x = new nRobot();
Behavior b1 = x.new goForward();//kewl,huh?
Behavior b2 = x.new goCrazy();
Behavior b3 = x.new doSound();
Behavior [] bArray = {b1, b3, b2};
Arbitrator arb = new Arbitrator(bArray);
arb.start();
}
i'm forced to use emu-lejosrun since school is shutdown. now when i run the code only the goCrazy() class is run. what little i've read lead me to think that the default behavior<sic> is for cycling through each at least once? i also tried some inner classes applying separate Arbitrator()'s as well with the same result, only one Behavior() like so:
- Code: Select all
public static void main(String [] args) {
nRobot x = new nRobot();// instance of current class
Arbitrator a = new Arbitrator(new Behavior[]{
x.new goForward(),x.new goCrazy()
});
a.start();
Arbitrator b = new Arbitrator(new Behavior[]{
x.new goCrazy()
});
b.start();
}
could someone please help? thanks in advanced.
w
