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:
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?
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 ); } } |