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

2015/10/28

Animation: The images will not switch back and forth

BroTolo BroTolo

2015/10/28

#
If the key "right" is held down, the character will move right, making the character switch to a moveRight image. Well while its moveRight, I'd like it to alternate between moveRight and moveRight2. Then after the key is released it defaults back to the "still" image (default)
private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
image1 = new GreenfootImage("charStill.png");
        image2 = new GreenfootImage("charShootRight.png");
        image3 = new GreenfootImage("charShootLeft.png");
        image4 = new GreenfootImage("charMoveRight1.png");
        image5 = new GreenfootImage("charMoveLeft1.png");
        image6 = new GreenfootImage("charMoveRight2.png");
        image7 = new GreenfootImage("charMoveLeft2.png");
if(Greenfoot.isKeyDown("right"))
        {
            move(4);
            setImage(image4);
            if (getImage() == image4)
            {
                setImage(image6);
            }
            else if (getImage() == image6)
            {
                setImage(image4);
            }
        } 
        else 
        {
           setImage(image1); 
        }
Super_Hippo Super_Hippo

2015/10/28

#
Line 4 sets the image to 'image4'. In the next line you compare the current image to image4. Of course it is image 4, so it is set to image6. That happens every act cycle. Only set it to 'image4' once when you are starting to move.
if (getImage() == image1) setImage(image4);
else if ...
BroTolo BroTolo

2015/10/28

#
What would i put in the else if? If it sets to image4 which is a running picture, id like image 4 and 6 to alternate, then on release change back to image1.
danpost danpost

2015/10/28

#
Easier, would be to ask if it is NOT a certain image; then, if it wasn't, set it to it:
if (Greenfoot.isKeyDown("right"))
{ 
    move(4);
    if (getImage() != image4) setImage(image4); else setImage(image6);
}
else if (Greenfoot.isKeyDown("left"))
{
    move(-4);
    if (getImage() != image5) setImage(image5); else setImage(image7);
}
else setImage(image1);
BroTolo BroTolo

2015/10/28

#
Is there a way to have the image up longer? so its not flickering crazily? Maybe a timer? btw thank you for all the help, You're a code wizard.
danpost danpost

2015/10/28

#
BroTolo wrote...
Is there a way to have the image up longer? so its not flickering crazily? Maybe a timer? btw thank you for all the help, You're a code wizard.
Sure. All you need is an 'int' field.
// instance field
private int timer;
//
timer++;
if (Greenfoot.isKeyDown("right"))
{
    move(4);
    // detect new image set
    if (getImage() != image4 && getImage() != image 6) timer = 0;
    // detect time to change image
    if (timer%10 == 0)
    { // change image
        if (timer%20 == 0) setImage(image4); else setImage(image6);
    }
else if (Greenfoot.isKeyDown("left"))
{
    etc.
BroTolo BroTolo

2015/10/28

#
Where do I place timer++? I'm sorry it just seemed a bit confusing, because currently it doesnt alternate, if it does. it takes forever. I have yet to see it alternate
BroTolo BroTolo

2015/10/28

#
Actually disregard, the reply above, but holding left is glitchy still. Right is smooth, left is glitchy
public void Movement()
    {
        timer++;
        if (Greenfoot.isKeyDown("right"))
        { 
            move(4);
            if (getImage() != image4 && getImage() != image6) timer = 0;
            if (timer%10 == 0)
            {
               if (timer%20 ==  0)
               {
                   setImage(image4);
               }
               else
               {
                   setImage(image6);
               }
            }        
        }
        else if (Greenfoot.isKeyDown("left"))
        {
            move(-4);
            if (getImage() != image5 && getImage() != image7) timer =0;
            if (timer%10 == 0)
            {
                if (timer%20 == 0)
                {
                    setImage(image5);
                }
                else
                {
                    setImage(image7);
                }
            }
            else
            {
                setImage(image7);
            }
        }
        else
        {
            setImage(image1);
        }
BroTolo BroTolo

2015/10/28

#
Nevermind, I got it, Sorry for the spam, thank you for the help!
danpost danpost

2015/10/29

#
Remove lines 35 through 38 from your last code post.
Super_Hippo Super_Hippo

2015/10/29

#
BroTolo wrote...
What would i put in the else if?
To answer this question (even if you don't need it anymore): I only replaced your line 4 and added the 'else' for the 'if' in the next line.
You need to login to post a reply.