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

2012/4/26

Animation Problems

Denzien Denzien

2012/4/26

#
I have been trying to impliment animation into the game I am working on, here is the code for it:
    public void Animation()
    {
        if(Greenfoot.isKeyDown("W"))
        {
            setImage("act_char_turn1.png"); 
            Greenfoot.delay(5); 
            setImage("act_char.png"); 
            Greenfoot.delay(5);
            setImage("act_char_turn-1.png"); 
            Greenfoot.delay(5); 
            setImage("act_char.png"); 
            Greenfoot.delay(5); 
        }
    }
But the delays seem to interfere with my movement code which is:
public void move()
{
     if(Greenfoot.isKeyDown("W"))
     {
          move(2);
     }
}
And the delays in the first bit of code delay the move(2); as well, how do I stop this? or is there another way of using animation?
SPower SPower

2012/4/26

#
The delay method of Greenfoot stops the entire scenario for a while. That's why you actually mustn't use it here....
Gazzzah Gazzzah

2012/4/26

#
I would do something like this: Firstly setup some variables (including images):
    public int sequence = 0;
    public GreenfootImage charA = new GreenfootImage("act_char_turn1.png");
    public GreenfootImage charB = new GreenfootImage("act_char.png");
    public GreenfootImage charC = new GreenfootImage("act_char_turn-1.png");
Then here is my version of your Animation method:
    public void Animation()
    {
        if(Greenfoot.isKeyDown("W"))
        {
            sequence ++; //Add 1 to 'sequence'
            switch (sequence) //Casewhere sequence == ...
            {
                case 1: setImage(charA); break;
                //wait 5 incriments of sequence (case 6)
                case 6: setImage(charB); break;
                case 11: setImage(charC); break;
                case 16: setImage(charB); break;
                //wait 4 inciments before reseting it to '0' (so it starts again)
                //waiting 4 instead of 5 to include the one from '0'
                case 20: sequence = 0; break;
            }
        }
        // if "W" is not pressed (I image the character is stopped)
        // Set the image to the standstill image
        else
        {
            if (getImage() != charB)
            {
                setImage(charB);
            }
        }
    }
Let me know if that's helpful / unhelpful or if you have any questions
SPower SPower

2012/4/26

#
What Gazzzah is saying should work.
tylers tylers

2012/4/26

#
or look at my animation one http://www.greenfoot.org/scenarios/4759
Denzien Denzien

2012/4/26

#
Thanks for this, It will help a lot ^^
You need to login to post a reply.