I have code working for movement of an actor and code that swaps between two images to give the appearance of the actor walking, but the swap happens extremely fast. how can I edit this code to swap the image on every other step to slow down the animation, if possible? I added comments specifying lines for this specific action.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Robo here. * * @author (your name) * @version (a version number or a date) */ public class Robo extends Actor { private Counter counter; private GreenfootImage image1 = new GreenfootImage ("robot.png");// this line private GreenfootImage image2 = new GreenfootImage ("robot2.png");//this line public Robo(Counter pointCounter) { counter= pointCounter; } /** * Act - do whatever the Robo wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { { checkKeys(); roboWalk();// this line removeSmiley(); } } /** * Method for movement via keypresses */ public void checkKeys() { if (Greenfoot.isKeyDown("left") == true) { turn(-5); } else if (Greenfoot.isKeyDown("right") == true) { turn(5); } if (Greenfoot.isKeyDown("up") == true) { move(1); } else if (Greenfoot.isKeyDown("down") == true) { move(-1); } if(Greenfoot.isKeyDown("a") == true) { turn(-90); move(1); turn(90); } if(Greenfoot.isKeyDown("d") == true) { turn(90); move(1); turn(-90); } if (Greenfoot.isKeyDown("w") == true) { move(1); } else if (Greenfoot.isKeyDown("s") == true) { move(-1); } String key = Greenfoot.getKey(); if ("l".equals(key)) dropBomb(); if ("j".equals(key)) dropBombLeft(); if ("i".equals(key)) dropBombUp(); if ("k".equals(key)) dropBombDown(); if ("7".equals(key)) dropBombUpLeft(); if ("9".equals(key)) dropBombUpRight(); if ("3".equals(key)) dropBombDownRight(); if ("1".equals(key)) dropBombDownLeft(); //if ("j".equals(Greenfoot.getKey())) //{ // dropBomb(); //} //if ("I".equals(Greenfoot.getKey())) //{ // dropBombUp(); //} // if ("k".equals(Greenfoot.getKey())) //{ // dropBomb(); //} } /** * Method to remove touching Smiley and add point */ public void removeSmiley() { if(isTouching(Smiley.class) == true) { Greenfoot.playSound("yippee.wav"); removeTouching(Smiley.class); counter.add(1); } if (counter.getValue() >= 5) { gameOverWin(); } } /** * Method to drop bomb */ public void dropBomb() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(0); } /** * Method to drop bomb up */ public void dropBombUp() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(-90); } /** * Method to drop bomb left */ public void dropBombLeft() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(180); } /** * Method to drop bomb up */ public void dropBombDown() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(90); } /** * Method to drop bomb */ public void dropBombUpLeft() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(-135); } /** * Method to drop bomb up */ public void dropBombUpRight() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(-45); } /** * Method to drop bomb left */ public void dropBombDownLeft() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(135); } /** * Method to drop bomb up */ public void dropBombDownRight() { //getWorld().addObject(new Bomb(), 100, 100); Bomb bomb = new Bomb(); getWorld().addObject(bomb, getX(), getY()); bomb.setRotation(45); } /** * Method to end game with a win screen and sound */ public void gameOverWin() { GreenfootSound sound1 = new GreenfootSound("ta da.wav"); sound1.play(); getWorld().addObject(new Victory(), 300, 200); while(sound1.isPlaying()) {} // wait till first sound finishes Greenfoot.playSound("victory.wav"); Greenfoot.stop(); } // this whole method: /** * Method to change the robot images */ public void roboImageSwap() { if (getImage().equals(image1)) { setImage (image2); } else { setImage (image1); } } // and this method: /** * Method to make the robot appear to walk */ public void roboWalk() { if(Greenfoot.isKeyDown("up") == true) { roboImageSwap(); } } }