I am trying to make actor move randomly each X seconds. The code for act is like this:
@Override
public void act(float delta)
{
super.act(delta);
if (delta > (float)(rand.nextInt(5)+3))
{
switch(rand.nextInt(3))
{
case 0:
moveBy(3,0);
break;
case 1:
moveBy(-3,0);
break;
case 2:
moveBy(0,3);
break;
case 3:
moveBy(0,-3);
break;
}
}
}
Debug shows only X increasing overtime, and it stops on ~103. What can the problem be? Random is inited in constructor with System.currentTimeMillis() seed.
Hello sorry for my English test this simple example aver if you want, change the final variable to adjust.
private Random random= new Random();
private float inttt = 0;
private float tiempo = 0;
private int randomT = 0;
private int randomN = 0;
private final int MAXTIEMPO = 15; // cambiar para aumentar o disminuir el tiempo que pasa como maximo entre un movimiento y otro.//changed to increase or decrease the time spent at most from one movement to another.
public int RandomNumber(float delta){
tiempo += delta;
if(randomT == 0 || randomN == 0){
random.setSeed(System.nanoTime() * (long)inttt);
this.randomN = random.nextInt(MAXTIEMPO);
inttt += randomN;
randomT = randomN;
Gdx.app.log("randomN", ""+randomN);
Gdx.app.log("randomT", ""+randomT);
Gdx.app.log("tiempo", ""+tiempo);
return 0;
}else if(randomT != 0 && tiempo >= randomT){
tiempo = 0;
randomT = 0;
Gdx.app.log("randomN", ""+randomN);
Gdx.app.log("randomT", ""+randomT);
Gdx.app.log("tiempo", ""+tiempo);
return randomN;
}else{
Gdx.app.log("randomN", ""+randomN);
Gdx.app.log("randomT", ""+randomT);
Gdx.app.log("tiempo", ""+tiempo);
return 0;
}
}
@Override public void act(float delta) { super.act(delta);
if (RandomNumber(delta) != 0){
switch(rand.nextInt(4)){
case 0:
moveBy(3,0);
break;
case 1:
moveBy(-3,0);
break;
case 2:
moveBy(0,3);
break;
case 3:
moveBy(0,-3);
break;
}
}