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

2024/1/2

An animation only takes the last photo of it?

Kyuubi Kyuubi

2024/1/2

#
In the following provided code, on the press of key "k" an animation should take place before spawning the magic attack, but unless i use something like greenfoot delay, only the last frame is shown, no matter how much time i put between the frames
if (Greenfoot.isKeyDown("k") && !kKeyPressed && Mana.mana >= 10&& magicCooldown==0) {
            kKeyPressed = true;
            imageToggleCooldown=0;
            for (int i = 0; i < 19; i++) {
            cycleImagesMagic();
        }
            Mana.instance.lmana(10);
            magicCooldown=200;
            spawnMagicAttack();
        }

        if (!Greenfoot.isKeyDown("k")) {
            kKeyPressed = false;
        }
    }
        private void cycleImagesMagic() {
         if (imageToggleCooldown == 0) {
        setImage("magic/MagicAttack" + magicAttack + ".png");
        magicAttack = (magicAttack + 1) % 4;
        imageToggleCooldown = 5;  // Adjust the cooldown as needed
        } else {
        imageToggleCooldown--;
        }
    }
to explain the code, I have 4 pictures that it should cycle thru with 5 acts cooldown between each of them, thats why the "i" in the for goes to 19. From 1 to 2, 5 acts, 2 to 3, 5 acts and 3 to 4, 5 acts, summed with the 4 acts needed to do the animation, its 19. Please help i dont know what else to do :(((
Kyuubi Kyuubi

2024/1/2

#
Reply: I kinda solved it in a very "unholy" way i used another variable that is set as 1 when k is pressed and if that variable is 1, the default animation becomes that animation, and once it hits 4 acts, it changes back to 0. It works but i feel like it isnt the right way
danpost danpost

2024/1/3

#
Kyuubi wrote...
Reply: I kinda solved it in a very "unholy" way i used another variable that is set as 1 when k is pressed and if that variable is 1, the default animation becomes that animation, and once it hits 4 acts, it changes back to 0. It works but i feel like it isnt the right way
Seems like an awful many variables. You should only need one for the state of the 'k' key and a timer for the animation:
// fields
boolean kKeyDown;
int kTimer;

// in act
if (kKeyDown != Greenfoot.isKeyDown("k")) {
    kKeyDown = ! kKeyDown;
    if (kKeyDown && Mana.mana >= 10 && kTimer == 0) {
        kTimer = 4*5+1;
        Mana.mana -= 10;
    }
}
if (kTimer > 0 && (--kTimer)%5 == 0) {
    if (kTimer == 0) {
        Greenfoot.delay(5);
        spawnMagicAttack();
    }
    else {
        setImage("magic/MagicAttack" + (4-(kTimer/5)) + ".png");
    }
}
You need to login to post a reply.