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

2017/4/16

Help with animating an actor

jcboz jcboz

2017/4/16

#
So I'm trying to animate an actor so that there is a walk cycle animation. I haven't yet tried to add if statements for when the animation should happen, but I am trying to figure out how to have a conintuous animation run when the play button is hit. So far I have
1
2
3
4
5
6
7
8
9
10
public void switchImage()  
    {
        int span = 2; // cycles between image changes
     
    imageTimer++;
    if (imageTimer == span) setImage(image2);
    if (imageTimer == 2*span) setImage(image3);
     
         
    }
The first three images play out nicely, but they don't repeat and stop once the third and final image is played. Thanks.
danpost danpost

2017/4/16

#
You just need to reset the value of 'imageTimer' back to zero when it reaches '2*span'. BTW, 2 cycles will happen quite quickly and will probably not be enough to remove jitters from the animated actor. You will probably need to adjust its value a bit.
Yehuda Yehuda

2017/4/16

#
danpost wrote...
You just need to reset the value of 'imageTimer' back to zero when it reaches '2*span'.
...And in the same if statement for that, you will also have to set the image back to image1. I would think that it should be at 3*span so that there is also an interval between the third image and the first. So I would add an if at line 8 like the following:
1
2
3
4
if (imageTimer == 3 * span) {
    setImage(image1); // I'm assuming that there is an 'image1'
    imageTimer = 0;
}
jcboz jcboz

2017/4/16

#
Thank you guys very much for the help! I was also able to successfully only have the animation play when the right key is pressed, so it looks like a running animation when moving right. However I tried flipping the images and having them work the same but for when he runs left but I'm having trouble getting the images to cylce through. Instead the actor only faces left in the default standing image instead of cycling through the running animation. Here is what I have, do you mind looking at it and seeing if it's an easy fix? Thanks
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public void move()
    {
       if (Greenfoot.isKeyDown("right"))
       {
           move(4);
           int span = 4;
        int imgCt = 3;
        imageTimer++;
        if (imageTimer == span)
        {
            setImage (image2);
        }
        if (imageTimer == 2*span)
        {
            setImage(image3);
        }
        if (imageTimer == 3 * span)
        {
            setImage (image1);
            imageTimer = 0;
        }
       }
       else
       {
           setImage (image1);   
       }
        
         
       if (Greenfoot.isKeyDown("left"))
       {
           move(-4);
           int span = 4;
           int imgCt = 3;
           imageTimer++;
           if (imageTimer == span)
           {
               setImage (image5);
           }
           if (imageTimer == 2*span)
           {
               setImage(image6);   
           }
           if (imageTimer == 3 * span)
           {
              setImage (image4);
              imageTimer = 0;
           }
           else
           {
              setImage (image4);   
           }
      }
    }
danpost danpost

2017/4/16

#
Remove lines 23 through 26 and 48 through 51 and add the following after line 52:
1
2
3
4
5
else
{
    if (getImage() == 5 || getImage() == 6) setImage(image4);
    if (getImage() == 2 || getImage() == 3) setImage(image1);
}
Yehuda Yehuda

2017/4/16

#
danpost wrote...
Remove lines 23 through 26 and 48 through 51 and add the following after line 52:
1
2
3
4
5
else
{
    if (getImage() == 5 || getImage() == 6) setImage(image4);
    if (getImage() == 2 || getImage() == 3) setImage(image1);
}
getImage() returns a GreenfootImage, so how can you see if it equals an int (number)? I don't see how your (danpost's) code makes any sense.
danpost danpost

2017/4/16

#
Yehuda wrote...
getImage() returns a GreenfootImage, so how can you see if it equals an int (number)? I don't see how your (danpost's) code makes any sense.
It is supposed to be:
1
2
3
4
5
else
{
    if (getImage() == image5 || getImage() == image6) setImage(image4);
    if (getImage() == image2 || getImage() == image3) setImage(image1);
}
(I think you knew that)
jcboz jcboz

2017/4/16

#
It works now for the left side but now the animation isn't cycling for the right side. I feel like I'm missing something simple haha
danpost danpost

2017/4/16

#
jcboz wrote...
It works now for the left side but now the animation isn't cycling for the right side. I feel like I'm missing something simple haha
Please post the revised code for further review.
Yehuda Yehuda

2017/4/16

#
That 'else' is only on the 'if (Greenfoot.isKeyDown("left"))', nothing to do with the first if for key presses.
Yehuda Yehuda

2017/4/16

#
danpost wrote...
I gave the appropriate placement for the suggested code.
If your code is after line 52 then when this method gets called the following will happen:
  • if the right key is pressed...
  • if imageTimer is at the right number...
  • set the image to 2, 3, or 1
  • since the right key is pressed, the left key is pressed == false
  • so we run the 'else'
  • if the image is 2 or 3, then set it to 1
So what you have here is that with the right animation, the image will never be 2 or 3, always 1. @danpost Now you destroyed your post entirely.
danpost danpost

2017/4/16

#
Yehuda wrote...
That 'else' is only on the 'if (Greenfoot.isKeyDown("left"))', nothing to do with the first if for key presses.
Just need to add 'else' at the beginning of line 29.
jcboz jcboz

2017/4/17

#
Thank you guys both very much! Adding the else statement at the begginging of line 29 fixed the issue. I appreciate the help.
You need to login to post a reply.