I want to make an animation for casting a fishing rod, but when I click the key to cast ("f"), it only plays 1 frame of the animation. I have to press the key several times for the full animation to play out. Heres my code:
public class Fisherman extends Actor
{
private int currentFrame = 0;
private int speed = 5;
GreenfootImage[] walkRight = new GreenfootImage[5];
GreenfootImage[] walkLeft = new GreenfootImage[5];
GreenfootImage[] castRight = new GreenfootImage[5];
GreenfootImage[] castLeft = new GreenfootImage[5];
String facing = "right";
SimpleTimer animationTimer = new SimpleTimer();
public Fisherman()
{
for (int i = 0; i < walkRight.length; i++)
{
walkRight[i] = new GreenfootImage("images/walkingAni/walking" + i + ".png");
walkRight[i].scale(100,80);
}
for (int i = 0; i < walkLeft.length; i++)
{
walkLeft[i] = new GreenfootImage("images/walkingAni/walking" + i + ".png");
walkLeft[i].mirrorHorizontally();
walkLeft[i].scale(100,80);
}
animationTimer.mark();
setImage(walkRight[0]);
}
int imageIndex = 0;
public void walk()
{
if(animationTimer.millisElapsed() < 200)
{
return;
}
animationTimer.mark();
if(facing.equals("right"))
{
setImage(walkRight[imageIndex]);
imageIndex = (imageIndex + 1) % walkRight.length;
}
else
{
setImage(walkLeft[imageIndex]);
imageIndex = (imageIndex + 1) % walkLeft.length;
}
}
public void cast()
{
for (int i = 0; i < castRight.length; i++)
{
castRight[i] = new GreenfootImage("images/castAni/cast" + i + ".png");
castRight[i].scale(100,80);
}
for (int i = 0; i < castLeft.length; i++)
{
castLeft[i] = new GreenfootImage("images/castAni/cast" + i + ".png");
castLeft[i].mirrorHorizontally();
castLeft[i].scale(100,80);
}
if(animationTimer.millisElapsed() < 200)
{
return;
}
animationTimer.mark();
if(facing.equals("right"))
{
setImage(castRight[imageIndex]);
imageIndex = (imageIndex + 1) % castRight.length;
}
else
{
setImage(castLeft[imageIndex]);
imageIndex = (imageIndex + 1) % castLeft.length;
}
}
public void castIt()
{
if (Greenfoot.isKeyDown("f"))
{
cast();
}
}
public void move()
{
int dx = 0;
if (Greenfoot.isKeyDown("d"))
{
dx++;
facing = "right";
walk();
}
if (Greenfoot.isKeyDown("a"))
{
dx--;
facing = "left";
walk();
}
setLocation(getX()+dx, getY());
if (isTouching(Boundary.class))
{
setLocation(getX()-dx, getY());
}
}
public void act()
{
castIt();
move();
}
}
