0
\$\begingroup\$

So I figured by myself (supposedly) how to make the attack animation cycle all the way through after pressing a key without needing to keep it pressed. But now it doesn't always start at the same frame index and sometimes doesn´t even complete the animation (as you will see, sometimes it does). Heres a gif of what's going on.

enter image description here

These go in the constructor

    aR = new Animation(50, Assets.aRight);
    aL = new Animation(50, Assets.aLeft);
    aF = new Animation(50, Assets.aFront);
    aB = new Animation(50, 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 true.

    attackTimer += System.currentTimeMillis() - lastAttackTimer;
    lastAttackTimer = System.currentTimeMillis();
    if(attackTimer < attackCooldown){
        timerFlag = false; 
        return;
    }

    if(!handler.getKeyManager().attack)
        return;

    timerFlag = true;   

(after a little bit of code attackTimer is set to 0)

To decide what the game will render I use an int (stillLock) that saves the last direction the player faced and I have a second static flag that indicates that the animation started. (I even reset the index every time it starts just to make sure *sadface)

else if(timerFlag&&!animCd){

        switch (stillLock){
        case 1:      //to attack
            animCd = true;
            timerFlag = false;
            aL.setIndex(0);
            return aL.getCurrentFrame();
        case 2:
            animCd = true;
            timerFlag = false;
            aR.setIndex(0);
            return aR.getCurrentFrame();
        case 3:
            animCd = true;
            timerFlag = false;
            aB.setIndex(0);
            return aB.getCurrentFrame();
        case 4:
            animCd = true;
            timerFlag = false;
            aF.setIndex(0);
            return aF.getCurrentFrame();
        }
    }
    else if(animCd){
        switch (stillLock){
        case 1:         //to finish attack animation
            aL.tick();
            return aL.getCurrentFrame();
        case 2:
            aR.tick();
            return aR.getCurrentFrame();
        case 3:
            aB.tick();
            return aB.getCurrentFrame();
        case 4:
            aF.tick();
            return aF.getCurrentFrame();
        }

And this is the Animation class where the magic happens

public class Animation {

private int speed, index;
private long lastTime, timer;
private BufferedImage[] frames;

public Animation(int speed, BufferedImage[] frames){
    this.speed = speed;
    this.frames = frames;
    index = 0;
    lastTime = System.currentTimeMillis();
    timer = 0;
}

public void tick(){
    timer += System.currentTimeMillis() - lastTime;
    lastTime = System.currentTimeMillis();

    if(timer > speed){
        index++;
        timer = 0;
        if(index >= frames.length){
                index = 0;
                Player.animCd = false;
        }
    }
}

public BufferedImage getCurrentFrame(){
    return frames[index];
}

public void setSpeed(int speed){
    this.speed = speed;
}

public void setIndex(int index){
    this.index = index;
}

}

And finally it renders like this.

public void render(Graphics g) 
{
    g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()),
            (int) (y - handler.getGameCamera().getyOffset()), width, height, null);
}

I'm trully sorry for the code dump, I swear I worked hard narrowing it down only to the important stuff and being specific. Hope you can help me, I've been stuck with this for weeks... :)

\$\endgroup\$
0
\$\begingroup\$

So I found the answer myself, it was actually really simple. I used a static flag to turn off the attack animation (animCd) but didn't realize other instances of Animation were running (even though attack animation was running) so it turned off before sometimes because of that. I added an extra condition so it was only turned off when they were attack animations. Hope other people find this helpful. Peace.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.