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

2018/9/28

I am having a serious problem with my animations.

CosmicCaleb CosmicCaleb

2018/9/28

#
I have multiple animations for a knight. I have
private int imageNumber = 1;
and
private int count;
elsewhere in the code.
public void switchPicone()
    {
        if (count % 10 == 1)
        imageNumber++;
        if (imageNumber == 4)
        {
            imageNumber = 1;
        }
        setImage("ka" + imageNumber + ".png");
        count++;
    }
I have an error that shows up at random points in the game. "Caused by: java.io.FileNotFoundException: Could not find file: ka5.png" The problem with this is that the game shouldn't even be searching for a ka5.png file. In line 5 of the code, it is basically saying to alternate between images ka1.png - ka4.png, but the program is searching for a 5th image. It is super annoying, and no one I have asked has seemed to know what the problem is. Does that make sense?
danpost danpost

2018/9/29

#
CosmicCaleb wrote...
In line 5 of the code, it is basically saying to alternate between images ka1.png - ka4.png, but the program is searching for a 5th image.
Lines 5 through 9 are not being restricted to when (count % 10 == 1). The only thing restricted by that condition is the increment of imageNumber. You need to use curly brackets to your code:
public void switchPicone()
{
    if (count % 10 == 1)
    { // here
        imageNumber++;
        if (imageNumber == 4)
        {
            imageNumber = 1;
        }
        setImage("ka" + imageNumber + ".png");
    } // and here
    count++;
}
A more concise way (without an imageNumber field is this:
public void switchPicone()
{
    count = (count+1)%40;
    if (count == 1) setImage("ka" +(count/10) + ".png");
}
You can even remove the call to switchPicone in your act method and replace it with lines 3 and 4, as is (also removing the switchPicone method, itself)
CosmicCaleb CosmicCaleb

2018/9/30

#
When I put the curly brackets in the code, the animation doesn't work at all. Also there are four pictures being rotated through. With the more concise code you gave me, it only goes to the first picture, and doesn't cycle through to the other 3.
Super_Hippo Super_Hippo

2018/9/30

#
I think instead of
if (count == 1) setImage("ka" +(count/10) + ".png");
it should be
if (count % 10 == 0) setImage("ka" +(count/10+1) + ".png");
danpost danpost

2018/10/1

#
Hippo is correct about what the line should be. You should have been getting an error without the correction since you are not using a "ka0.png" image.
You need to login to post a reply.