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

2015/11/22

Help with walking Animation?

BigSim BigSim

2015/11/22

#
Ok So I have this method that's supposed to create a walking animation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void animate()
    {
        if (animState.equals("idle"))
        {
            setImage( idle );
        }
        else if(animState.equals("walk") )
        {
             setImage( leftWalk );
             count--;
            if( count == 0 )
            {
                setImage( rightWalk );
                count = 5;
            }
          
        }
I think the problem is that the image is being shown too fast to be visible. How do I delay the switch times? P.S, Count is a private class variable that is set to 50
danpost danpost

2015/11/22

#
Try changing line 14 to:
1
count = 10; // or 'count = 15;'
If that does not help, show how 'rightWalk' and 'leftWalk' are being declares and assigned values (to start).
BigSim BigSim

2015/11/23

#
BigSim BigSim

2015/11/23

#
1
2
3
GreenfootImage idle = new GreenfootImage("Shooter1_Stand.png");
   GreenfootImage leftWalk = new GreenfootImage("Shooter1_LeftWalk.png");
   GreenfootImage rightWalk = new GreenfootImage("Shooter1_RightWalk.png");
BigSim BigSim

2015/11/23

#
Setting it to 15 did not work
danpost danpost

2015/11/23

#
BigSim wrote...
Setting it to 15 did not work
Okay. Now, let us look at the 'animState' field -- how it is declared, set and used (all codes using that field). The 'animate' method does not need to be shown if it has not changed from the above.
BigSim BigSim

2015/11/23

#
1
private String animState = "idle";
BigSim BigSim

2015/11/23

#
its an instance variable
BigSim BigSim

2015/11/23

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void userInput()
    {
        
       if (Greenfoot.isKeyDown( keyLeft ))
        {
            turn(-5);
        }
       if (Greenfoot.isKeyDown( keyRight ))
        {
            turn(5);
        
       if (Greenfoot.isKeyDown( keyDown ) )
       {
          move( -speed );
        }
       if (Greenfoot.isKeyDown( keyUp ) )
       {
         move( speed );
         animState = "walk";
         
        }
         
        if (! (Greenfoot.isKeyDown( keyLeft ) ||Greenfoot.isKeyDown( keyRight ) || Greenfoot.isKeyDown( keyUp ) || Greenfoot.isKeyDown( keyDown)) )
        {
            animState = "idle";
        }
    }
danpost danpost

2015/11/23

#
Okay. When 'idle' no key except the 'keyUp' key will change the image to a walking image. When walking, all keys must be released for the image to return to 'idle'. The walking image will change with the 'animate' code; but the 'count' field will only be zero for one act cycle. This means that one walking image will only last for one act cycle and the other the rest of the time (the one act cycle will probably not be enough for you to visually see the change unless you slow down the scenario speed. I think if you move line 13 of the 'animate' method to a better place, it should function as you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void animate()
{
    if (animState.equals("idle"))
    {
        setImage( idle );
    }
    else if(animState.equals("walk") )
    {
        count++;
        if ( count == 16 )
        {
            count = 0;
        }
        if ( count/8 == 0 )
        {
            setImage( leftWalk );
        }
        else
        {
            setImage( rightWalk );
        }
    }
}
BigSim BigSim

2015/11/23

#
Thank you so much, but could you explain that else if part? It works, but I don't know why it does.
danpost danpost

2015/11/23

#
There are (I believe) only two animation states that 'animState' could hold -- "idle" and "walk". Being there are only two, line 7 can be reduced to:
1
else
But, that has nothing to do with why it was not previously working. You needed both images, 'leftWalk' and 'rightWalk', to show for more than just one act cycle (not just one of them). The code you had above only had the 'rightWalk' image show when the counter was zero. When the counter was not zero (for all other act cycles), the walking image shown was the 'leftWalk' image.
BigSim BigSim

2015/11/23

#
Ohhhh ok. I get it now. Thank you for helping me
You need to login to post a reply.