i am currently trying to make a game where when either the left or right arrow key is pressed, the image of the actor changes to make it appear to be walking. however, not only do the images not change but the actor is going the wrong way! Here's the code i've posted:
any reasons why this isn't working?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Stewie here. * * JUWAN * */ public class Stewie extends Actor { public Stewie() { setImage(img); } GreenfootImage img = new GreenfootImage("stewieMain.png"); GreenfootImage wr1 = new GreenfootImage("stewie1.png"); GreenfootImage wr2 = new GreenfootImage("stewie2.png"); GreenfootImage wl1 = getLeft1(); GreenfootImage wl2 = getLeft2(); /** * Act - do whatever the Stewie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if ( Greenfoot.isKeyDown("left") ) { setLocation(getX() - 2, getY()); walkLeft(); } else if ( Greenfoot.isKeyDown("right") ) { setLocation(getX() + 2, getY()); walkRight(); } else setImage(img); } public GreenfootImage getLeft1() { GreenfootImage a; flip(); a = wr1; return a; } public GreenfootImage getLeft2() { GreenfootImage a; flip2(); a = wr1; return a; } public void flip() { wr1.mirrorVertically(); } public void flip2() { wr2.mirrorVertically(); } public void walkLeft() { int a = 1; int b = a; if ( b != 0 ) { b--; setImage(wl1); } else if ( b == 0 ) { setImage(wl2); b = a; } } public void walkRight() { int a = 1; int b = a; if ( b != 0 ) { b--; setImage(wr1); } else if ( b == 0 ) { setImage(wr2); b = a; } } }