0

The object is jerking around when using the turn keys instead of a smooth movement.

I've tried moving around the two 4 and 6 keys (left and right) to else if and while statements inside and out of the main if statement.

    move(4);
    if(Greenfoot.isKeyDown("4"))
    {
        turn(-3);
    }
    if(Greenfoot.isKeyDown("6"))
    {
        turn(3);
    }
    if(Greenfoot.isKeyDown("8"))
    {
        move(4);
    }
    else if(Greenfoot.isKeyDown("5"))
    {
        move(2);
    }
    else if(Greenfoot.getRandomNumber(100) < 10)
    {
        turn(Greenfoot.getRandomNumber(90) - 45);
    }
    else if(getX() <= 5 || getX() >= getWorld().getWidth() -5)
    {
        turn(180);
    }
    else if(getY() <= 5 || getY() >= getWorld().getHeight() -5)
    {
        turn(180);
    }

I need to keep the 8 keybing and everything below it so it has both multi and single player capabilities, but also need the turn keys to work smoothly without having to continuously having to hold either 8 or 5.

TravJ14
  • 27
  • 8

1 Answers1

0

If neither 5 nor 8 is held, you have a 10% random chance of turning between -45 and 45 degrees. Is this the cause of what you describe as jerking around? I'm not sure you want that random turn if you also want keyboard controlled turning for this actor, so I think you just want to remove the block:

    else if(Greenfoot.getRandomNumber(100) < 10)
    {
        turn(Greenfoot.getRandomNumber(90) - 45);
    }
Neil Brown
  • 3,558
  • 1
  • 27
  • 36