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

2011/11/2

Alternatives to Greenfoot.delay()?

craigC craigC

2011/11/2

#
Hello, I'm trying to iron out an animation kink I ran into. I've implemented an array which holds the frames of an animation sequence, such as an attack move. I've been using delay() function to control the attack animation but I don't like how it freezes up everything else while the object which invoked the function animates. I would like other objects to keep animating also. Is there another function or method that'll help me with my problem? Here's an example of my attack animation code to help clarify my problem:
attDir = 0;
        if(Greenfoot.isKeyDown("b")) {
            if(dir == 0) {
                attDir = dir;
            } else {
                attDir = dir+7;
            }
            setImage( attack[0+attDir] );
            Greenfoot.delay(2);
            setImage( attack[1+attDir] );
            Greenfoot.delay(2);
            setImage( attack[2+attDir] );
            Greenfoot.delay(2);
            setImage( attack[3+attDir] );
        }
bourne bourne

2011/11/2

#
Something similar to the following will work:
private int attacking = -1;
    private int animationSpeed = 2;

    public void act()
    {
        if (attacking != -1)
        {
            if (dir == 0)
                attDir = dir;
            else
                attDir = dir + 7;
            setImage(attack[attacking / animationSpeed + attDir]);
            if (++attacking > 3 * animationSpeed)
                attacking = -1;
        }
        else if (Greenfoot.isKeyDown("b"))
            attacking = 0;
    }
craigC craigC

2011/11/2

#
Thanks bourne, that worked rather well. That's the kind of solution I was looking for.
darkmist255 darkmist255

2011/11/3

#
Just out of curiosity could you explain: setImage(attack); if (++attacking > 3 * animationSpeed) attacking = -1; Thanks :D!
bourne bourne

2011/11/3

#
Sure, The "attacking" variable simply acts as an index for the array of frames (images) of the animation. So since there are four images, we would want to go from index 0 to index 3. And so this variable would need to be incremented each step (++attacking). The "animationSpeed" variable is used to add duration for each frame. "attacking / animationSpeed" will return 0 up until "attacking" becomes greater than or equal to "animationSpeed" since it takes the integer part from it. For example: 0/2 = 0 1/2 = 0 2/2 = 1 3/2 = 1 4/2 = 2 5/2 = 2 6/2 = 3 7/2 = 3 8/2 = 4 (can see this last index is undesirable) So actually the if statement should be (so to have each frame displayed for the same duration) : if (++attacking >= 3 * (animationSpeed + 1) - 1) Where 3 is the top index. Can follow the example operations above for how this works. "attacking = -1;" stops the animation since we have "(attacking != -1)" When "attacking" equals -1, basically means an off status of this particular execution May wish to set the image back to a non-attacking image at this point.
darkmist255 darkmist255

2011/11/4

#
Thanks :D!
You need to login to post a reply.