I am trying to aniamate the character in my game using image arrays.
The idle animation is supposed to play all the time when no button is pressed.
If left or right is pressed, the run animation is supposed to play.
And when space is pressed, the shoot animation is supposed to play.
When the idle animation plays, nothing else plays for some reason, even if a button is pressed, and when the idle animation should in theory not even play because the if should prevent it.
The shoot animation shouldn't loop. When I press space, it depends on how long I press it on what frame it ends, and it never plays through.
How can I make the animations work simultaniously without overriding each other and creating a mess?
Here is the code of my character class:
And here is 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import greenfoot.*; public class Character extends Mover { private int shotTimer = 15 ; private int SpaceTimer = 15 ; private int AnyPressed = 0 ; private int idleFrames = 35 ; private int idleTime; private int ShootFrames = 6 ; private int ShootTime; private int ShootAnimate; private int LeftFrames = 16 ; private int LeftTime; private int RightFrames = 16 ; private int RightTime; Character() { } public void act() { shotTimer ++; AnyPressed --; SpaceTimer --; ShootAnimate --; checkKeys(); } private void checkKeys() { if (Greenfoot.isKeyDown( "left" ) ) { animateLeft(); moveLeft(); AnyPressed = 50 ; } else { AnyPressed = 0 ; } if (Greenfoot.isKeyDown( "right" ) ) { animateRight(); moveRight(); AnyPressed = 50 ; } else { AnyPressed = 0 ; } if (Greenfoot.isKeyDown( "space" ) && SpaceTimer <= 0 ) { ShootAnimate= 5 ; if (shotTimer > 25 ){ ShootTime = 0 ; this .shoot(); shotTimer = 0 ; AnyPressed = 50 ; } SpaceTimer = 25 ; } else { AnyPressed = 0 ; } if (ShootAnimate >= 1 ){ animateShoot(); } /*if(AnyPressed < 1 /*&& ShootAnimate >= 1){ setImage(animateIdle()[idleTime]); idleTime++; if(idleTime == idleFrames){ idleTime = 0; } }else{ AnyPressed = 1; } */ } public void animateShoot(){ setImage(animateShootx()[ShootTime]); if (ShootFrames < 6 ){ ShootTime ++; } /*if(ShootTime == ShootFrames){ ShootTime = 0; }*/ } public void animateLeft(){ setImage(animateLeftx()[LeftTime]); LeftTime ++; if (LeftTime == LeftFrames){ LeftTime = 0 ; } } public void animateRight(){ setImage(animateRightx()[RightTime]); RightTime ++; if (RightTime == RightFrames){ RightTime = 0 ; } } public void shoot() { this .getWorld().addObject( new Bullet(), this .getX()+ 30 , this .getY()- 45 ); } public GreenfootImage[] animateIdle(){ GreenfootImage[] images = new GreenfootImage[idleFrames]; for ( int i = 0 ; i < 35 ; i++){ if (AnyPressed < 1 ){ images[i] = new GreenfootImage( "Idle" + i + ".png" ); } } return images; } public GreenfootImage[] animateShootx(){ GreenfootImage[] images = new GreenfootImage[ShootFrames]; for ( int i = 0 ; i < 6 ; i++){ if (ShootAnimate >= 1 ){ images[i] = new GreenfootImage( "Shoot" + i + ".png" ); } } return images; } public GreenfootImage[] animateLeftx(){ GreenfootImage[] images = new GreenfootImage[LeftFrames]; for ( int i = 0 ; i < 16 ; i++){ images[i] = new GreenfootImage( "Left" + i + ".png" ); } return images; } public GreenfootImage[] animateRightx(){ GreenfootImage[] images = new GreenfootImage[RightFrames]; for ( int i = 0 ; i < 16 ; i++){ images[i] = new GreenfootImage( "Right" + i + ".png" ); } return images; } } |
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 | import greenfoot.*; public class Mover extends Actor { private static final int speed = 6 ; // 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; } } |