This site requires JavaScript, please enable it in your browser!
Greenfoot back
LobsterL
LobsterL wrote ...

2018/2/18

Animating on movement

LobsterL LobsterL

2018/2/18

#
can i please get help it say java.lang.ArrayindexOutOfBoundsException: 9
LobsterL LobsterL

2018/2/18

#
import greenfoot.*;


/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    private boolean triggeredup;
    private boolean triggereddown;
    private boolean triggeredleft;
    private boolean triggeredright;
    private int frameCounterright = 0;
    private String[] gangright = {"gangright0.png","gangright1.png","gangright2.png","gangright3.png","gangright4.png","gangright5.png","gangright6.png","gangright7.png","gangright8.png"};
    private GreenfootImage myImage = getImage();
    int xPos;
    int yPos;
    public void addedToWorld(World w)
    {
       xPos = getX();
       yPos = getY();
    }
    public Crab()
    {
        triggeredup=false;
        triggereddown=false;
        triggeredright=false;
        triggeredleft=false;
    }
    public void act()
    {
       if (Greenfoot.isKeyDown("right"))
       {
           move (4);
           triggeredright=true;
           animateOnMoveright();
       }
       if (Greenfoot.isKeyDown("left"))
       {
           move (-4);
           triggeredleft=true;
       }
       if (Greenfoot.isKeyDown("up"))
       {
           setLocation(getX(), getY()-4);
           triggeredup=true;
       }
       if (Greenfoot.isKeyDown("down"))
       {
           setLocation(getX(), getY()+4);
           triggereddown=true;
       }
               if(isTouching(Food.class))
       {
            removeTouching(Food.class);
       }
    }
    public void animateOnMoveright()
    {
        if (getX()>=xPos|| getY()>=yPos)
       {
        setImage(gangright[frameCounterright]);
        frameCounterright++;
        if(frameCounterright > gangright.length)
          frameCounterright=0;
        
       }

    }
}
Vercility Vercility

2018/2/18

#
you array has length 9 but that means your max index is 8 because arrays start at 0. You'll need to change
if(frameCounterright > gangright.length)
to
if(frameCounterright >= gangright.length)
Might just as well use == actually.
LobsterL LobsterL

2018/2/18

#
thanks so much dude it works now... appreciate you
You need to login to post a reply.