There's a couple of things that stand out in your code. First, in one place you set the variable number
to a random value between 1 & 4, but then you're switching based on a variable named numero
. I'm guessing this is a typo, but if it's not, it's going to be a problem.
Regarding the shaking, consider how often you are calling act()
. If it gets called every time through the game loop (from an update()
or something similar), the result is you're picking a new direction too frequently (very likely multiple times per second). The solution to this is to keep track of how long you've been travelling in a particular direction & only pick a new direction when you've gone far enough.
As for leaving the screen, you need to add some sort of bounds checking. As originally posted, there's no code that checks to see if the actor has travelled off screen.
Here's an example of modifications that address these issues. I've included some comments to point out the reason for the various changes
@Override
public void act(float delta) {
super.act(delta);
// move in the direction of the current random heading
switch (randomHeading){
case 1: this.setX(this.getX()-delta*80);break;
case 2: this.setY(this.getY()-delta*80);break;
case 3: this.setX(this.getX()+delta*80);break;
case 4: this.setY(this.getY()+delta*80);break;
}
// if needed, stop at screen boundary & clear the heading
int maxX = Gdx.graphics.getWidth() - getWidth();
int maxY = Gdx.graphics.getHeight() - getHeight();
if(getX() < 0){
setX(0);
randomHeading = 0;
}
else if(getX() > maxX){
setX(maxX);
randomHeading = 0;
}
if(getY() < 0){
setY(0);
randomHeading = 0;
}
else if(getY() > maxY){
setY(maxY);
randomHeading = 0;
}
// update time spent on current heading & check if a new heading is needed
// recall that if the actor attempted to move off screen, randomHeading was set to zero
timeTravelled += delta;
float maxTravelTime = 10;
if(timeTravelled > maxTravelTime || randomHeading == 0){
timeTravelled = 0;
randomHeading = (int)(Math.random()*4+1);
}
Rectangle.set(this.getX(),this.getY(),this.getWidth(),this.getHeight());
}
For these changes to work, you'll need to add the following fields to your class:
int randomHeading
float timeTravelled
You will also probably want to adjust the value assigned to maxTravelTime
.
Without the rest of your code I can't check my solution against a compiler, but hopefully this is enough to help you out.
number
but theswitch
statement is aboutnumero
(undeclared). Maybe a typo? \$\endgroup\$