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

2021/7/8

How to properly animate my character?

BaseEvoli BaseEvoli

2021/7/8

#
I am creating a game in which I want to have an animated character. I have all the animations in GIF format (walking left, walking right, idle, shooting with the slingshot, and transitions between walking and idle and vice versa) The first problem is that the GIF I use 1. half of the time "softlocks" the project for some reason (as soon as I try to load the level where my character is in it just freezes, when I hit reset it will just grey out and not do anything) 2. doesn't play the 1/1000 times it does actually load. I used the GifImage class that is featured within Greenfoot, and the method resume should (in theory) start the animation, right? So why does it not play? Here is my character class:
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
54
55
56
57
58
59
import greenfoot.*;
 
/**
 * Character that wants to shoot balloons using a slingshot.
 */
 
public class Character extends Mover
{
    private int shotTimer = 15;
    private GifImage gif;
    private String image;
     
    public Character()
    {
       this.image = image;
       gif = new GifImage("Idle.gif");
        
       setImage(gif.getCurrentImage());
    
       gif.resume();
    }
     
    public void act()
    {
                
        checkKeys();
         
        shotTimer = shotTimer + 1;
    }
 
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
 
            moveLeft();
        }
 
        if (Greenfoot.isKeyDown("right") )
        {
 
            moveRight();
        }
 
        if (Greenfoot.isKeyDown("space") && shotTimer > 15)
        {
 
            this.shoot();
            shotTimer = 0;
        }
    }   
 
    public void shoot()
    {
        this.getWorld().addObject(new Bullet(), this.getX(), this.getY()+60);
  
    }
     
}
And the second question, how to I implement the animations smart? I want to have smooth transitions, and with instantly switching the files as soon as another key is pressed it becomes chopped up. Should I make a timer that blocks all inputs until the animation is finished? Or is there a smarter way to do it? What do you think?
Gbasire Gbasire

2021/7/8

#
can I see the code of the mover class ?
BaseEvoli BaseEvoli

2021/7/9

#
Gbasire wrote...
can I see the code of the mover class ?
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
import greenfoot.*;
 
/**
 * The class Mover provides some basic movement methods. Use this as a superclass
 * for other actors that should be able to move left and right.
 */
public class Mover extends Actor
{
    private static final int speed = 10;             // running speed (sideways)
     
    private int vSpeed = 0;                         // current vertical speed
     
    public void moveUp()
    {
       setLocation(getX(), getY()+5);
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
     
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
     
    private void gameEnd()
    {
        Greenfoot.stop();
    }
}
Super_Hippo Super_Hippo

2021/7/9

#
I never worked with GIFs but I think you need to call “setImage(gif.getCurrentImage());” from your act method and not only once when the object is created.
Gbasire Gbasire

2021/7/9

#
Yeah, that's exactly what Hippo says, you must do it in the act method, not in the constructor. By the way, GIFs don't work well on web, I would recommend using GreenfootImage arrays, if you're comfortable doing conversions and this type of stuff.
BaseEvoli BaseEvoli

2021/7/10

#
Could you elaborate on image arrays? Is it using the setImage method for every frame? Like manual animation?
Gbasire Gbasire

2021/7/10

#
of course : for example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public int imageNumber = 20; //number of images, from image 0 to image 19
public int imageTime;
public void act()
{
    setImage(animateImages()[imageTime]);
    imageTime++;
    if(imageTime == imageNumber)
        imageTime = 0;
}
 
public GreenfootImage[] animateImages()
{
    GreenfootImage[] images = new GreenfootImage[imageNumber]
    for(int i = 0; i < images.length; i++)
    {
        //if your images are named image0.png, image1.png etc
        images[i] = new GreenfootImage("image" + i + ".png");
    }
    return images;
}
danpost danpost

2021/7/11

#
You may want to check out my Animation support class (either for reference or for usage). You can find it here.
You need to login to post a reply.