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

2018/4/30

Adding an attack animations

OfficerRiot OfficerRiot

2018/4/30

#
I've added it so when I press x it starts the animation but whenever I press it while moving the all of the animations just stop and it's like the sprite is frozen.
danpost danpost

2018/4/30

#
Most of the time, with programming issues, No Code = No Help. That means related code, at bare minimum. Most times, more that that is required, however.
OfficerRiot OfficerRiot

2018/4/30

#
Heres the code public void attack() { if (Greenfoot.isKeyDown("x")) { imageCounter = (++imageCounter)%2; // change '2' higher to slow down animation if (imageCounter == 0) attackswitch(); } } public void attackswitch() { frame++; switch (frame){ case 1: setImage(Attack1); break; case 2: setImage(Attack2); break; case 3: setImage(Attack3); break; case 4: setImage(Attack4); break; case 5: setImage(Attack5); break; case 6: frame = 0; } }
danpost danpost

2018/4/30

#
OfficerRiotwhile moving[/quote wrote...
I do not see any movement code.
OfficerRiot OfficerRiot

2018/4/30

#
danpost wrote...
OfficerRiotwhile moving[/quote wrote...
I do not see any movement code.
Sorry heres the movement code ** * Moves Dooku * */ public void dookuforward() { if (Greenfoot.isKeyDown("d")) { move(3); imageCounter = (++imageCounter)%2; // change '2' higher to slow down animation if (imageCounter == 0) DookuswitchImageright(); } if (Greenfoot.isKeyDown("a")) { move(-3); imageCounter = (++imageCounter)%2; // change '2' higher to slow down animation if (imageCounter == 0) DookuswitchImageleft(); }
danpost danpost

2018/5/1

#
Now I can see why the animation might freeze when "x" and either "a" or "d" are held down. They both run the image animation. The absolute freezing is due to the fact that your image timer is using 2 as its limiting value. Changing it to an odd number might allow something to happen; but, still, it probably will not be what is wanted either. You already have 3 image sets competing with each other to be the animation of the actor (the normal moving left and right animations and the attacking animation). You probably have individual attacking left and attacking right sets as well. Without a control field or two, it would be almost impossible to control the animations properly. Also, to gain better/simpler control of the running of the animations, arrays should be used to hold the animation image sets (instead of individual fields for each image). If the timer was allowed to run regardless of a key being down or not (this could be done by adding a non-moving animation set, even it if were only to contain one single image), then double running can be avoided. So, what is needed, at minimum, are arrays for the images, for example:
GreenfootImage[] attackAnim = { Attack1, Attack2, Attack3, Attack4, Attack5 };
where you create the images here instead of in the fields used here and those fields are removed. All animatiton sets should be set up this way. The still left and still right animation sets can be created by using one image from each of the moving left and moving right animation sets. Another thing that will be needed is an array field to hold the animation set that is currently running:
GreenfootImage[] currentAnim = stillRightAnim;
Probably setting it to the still right animation set is the correct initial setting as a rotation of zero is equivalent to facing right. Now, in the constructor, you would want to set the initial image to the actor:
setImage(currentAnim[0]);
and in the action code, increment the timer, first thing:
imageCounter = (++imageCounter)%(2*currentAnim.length); // make '2' higher to slow down animation
Nowhere later on is this to be incremented. It will only need to be zeroed when the current animation image set is changed. Next, for movement, decide which way to move first -- before moving at all -- then move:
int direction = 0; // assume not moving
if (Greenfoot.isKeyDown("a")) direction--; // tend left
if (Greenfoot.isKeyDown("d")) direction++; // tend right
move(direction*3);
Note that, here, if both keys are down (or neither key is down) that the value of direction will be zero. The direction variable will be used later to determine, if need be, what image set to change to. Next, the attack:
if (Greenfoot.isKeyDown("x"))
{
    if (currentAnim != attackLeftAnim && currentAnim != attackRightAnim) // new attack
    {
        if (direction != 0) // direction by key
            currentAnim = direction == 1 ? attackRightAnim : attackLeftAnim;
        else // no key -- direction by current animation
            currentAnim = (currentAnim == stlllRightAnim || currentAnim == movingRightAnim) ? attackRightAnim : attackLeftAnim;
        imageCounter = 0;
    }
    else // continued atttack
    {
        if (currentAnim == attackLeftAnim && direction == 1) // change to facing right
        {
            currentAnim = attackRightAnim;
            setImage(currentAnim[imageCounter/(2*currentAnim.length)]);
        }
        if (currentAnim == attackRightAnim && direction == -1) / change to facing left
        {
            currentAnim = attackLeftAnim;
            setImage(currentAnim[imageCounter/(2*currentAnim.length)]);
        }
    }
}
else // not attacking
{
    if ((currentAnim == attackLeftAnim || currentAnim == attackRightAnim) && imageCounter == 0) // end attack
    {
        if (direction == 0) currentAnim = currrentAnim == attackLeft ? stillLeftAnim : stillRightAnim;
        else currentAnim = direction == 1 ? movingRightAnim : movingLeftAnim;
    }
    else if (direction == 0 && (currentAnim == movingRightAnim || currentAnim == movingLeftAnim) // stopping
    {
        currentAnim = currentAnim == movingRightAnim ? stillRightAnim : stillLeftAnim;
        imageCounter = 0;
    }
    else if (direction == 1 && currentAnim != movingRightAnim || direction == -1 && currentAnim != movingLeftAnim) // start moving or change directions
    {
        currentAnim = direction == 1 ? movingRightAnim : movingLeftAnim;
        imageCounter = 0;
    }
}
if (imageCount%(2*currentAnim.length) == 0) setImage(currentAnim[imageCount/(2*currentAnim.length)]);
The '2's should probably be put in a field and reference at each location of usage. As is, if you change any, you must change all of them. I hope I got everything in there as this was just done in the reply editor and is not tested. However, the idea should be apparent.
You need to login to post a reply.