In my program, I've tried to make an actor shake using Greenfoot.getRandomNumber(). My code is the following
However, the actor seems to shake in a single direction; it is always above and to the left of its original position, or below and to the right, never above and to the right or below and to the left. Does anyone know what might be causing this? Help is much appreciated.
if (((GameBackdrop) getWorld()).nervousOn)
{
shakeX += Greenfoot.getRandomNumber(3) - 1;
shakeY += Greenfoot.getRandomNumber(3) - 1;
if (shakeX > 25)
{
shakeX = 24;
}
if (shakeX < -25)
{
shakeX = -24;
}
if (shakeY > 25)
{
shakeY = 24;
}
if (shakeY < -25)
{
shakeY = -24;
}
}
setLocation(getX() + shakeX, getY() + shakeY);
