How To Set Attack Animation In Java
And so I figured by myself (supposedly) how to brand the attack animation bike all the way through after pressing a key without needing to keep it pressed. But at present it doesn't always start at the aforementioned frame index and sometimes doesn´t even complete the animation (as yous will see, sometimes it does). Heres a gif of what'south going on.
These become in the constructor
aR = new Animation(l, Assets.aRight); aL = new Blitheness(50, Assets.aLeft); aF = new Animation(50, Assets.aFront); aB = new Animation(l, Assets.aBack);
I use this timer to keep track of the cooldown and also set the flag (timerFlag) that indicates the start of the attack animation to truthful.
attackTimer += System.currentTimeMillis() - lastAttackTimer; lastAttackTimer = Arrangement.currentTimeMillis(); if(attackTimer < attackCooldown){ timerFlag = false; return; } if(!handler.getKeyManager().assail) return; timerFlag = truthful;
(afterward a little chip of lawmaking attackTimer is set to 0)
To decide what the game will render I use an int (stillLock) that saves the terminal direction the player faced and I have a second static flag that indicates that the animation started. (I even reset the alphabetize every fourth dimension it starts just to make sure *sadface)
else if(timerFlag&&!animCd){ switch (stillLock){ case one: //to attack animCd = true; timerFlag = imitation; aL.setIndex(0); return aL.getCurrentFrame(); case 2: animCd = true; timerFlag = simulated; aR.setIndex(0); return aR.getCurrentFrame(); instance 3: animCd = true; timerFlag = imitation; aB.setIndex(0); return aB.getCurrentFrame(); example 4: animCd = true; timerFlag = false; aF.setIndex(0); return aF.getCurrentFrame(); } } else if(animCd){ switch (stillLock){ case one: //to finish assail animation aL.tick(); return aL.getCurrentFrame(); case 2: aR.tick(); return aR.getCurrentFrame(); case 3: aB.tick(); return aB.getCurrentFrame(); example iv: aF.tick(); render aF.getCurrentFrame(); }
And this is the Animation form where the magic happens
public class Animation { private int speed, index; private long lastTime, timer; private BufferedImage[] frames; public Blitheness(int speed, BufferedImage[] frames){ this.speed = speed; this.frames = frames; index = 0; lastTime = Arrangement.currentTimeMillis(); timer = 0; } public void tick(){ timer += System.currentTimeMillis() - lastTime; lastTime = System.currentTimeMillis(); if(timer > speed){ index++; timer = 0; if(alphabetize >= frames.length){ index = 0; Player.animCd = simulated; } } } public BufferedImage getCurrentFrame(){ return frames[index]; } public void setSpeed(int speed){ this.speed = speed; } public void setIndex(int alphabetize){ this.index = index; }
}
And finally information technology renders like this.
public void render(Graphics g) { m.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, elevation, zip); }
I'm trully sorry for the code dump, I swear I worked hard narrowing information technology downwards but to the important stuff and being specific. Hope y'all tin help me, I've been stuck with this for weeks... :)
Source: https://gamedev.stackexchange.com/questions/133167/how-to-get-the-attack-animation-cycle-all-the-way-through-after-one-keyevent-wit
Posted by: martindanythe.blogspot.com
0 Response to "How To Set Attack Animation In Java"
Post a Comment